Called copy function, a is the definition operator()
of the object.
Functor of three advantages:
- Functor can have member functions and member variables. As is usually implemented in a class, you can add other members.
- Each functor has its own type. Templated can be achieved, namely to achieve multiple states.
- Functor is usually faster than the general function, because usually at compile time to complete many of the details.
Example 1:
#include<iostream>
using namespace std;
struct funtion {
void operator()(int i) {
cout << "i = " << i << endl;
}
};
int main()
{
funtion CPrint;
CPrint(10);//像函数一样去使用
CPrint.operator()(10);//显示调用
return 0;
}
Implementation of the results
analysis
form here is the general call.
Example 2:
#include<iostream>
#include<list>
#include<algorithm>
#include <iterator>
using namespace std;
typedef list<int> List;
class funtion {
private:
int value;
public:
funtion(int v):value(v){ }
void operator()(int& elem)const {
elem += value;
cout << "elem" << elem << endl;
}
};
int main()
{
List _list;
for (int i = 0; i <= 10; i++)
_list.push_back(i);
for_each(_list.begin(), _list.end(), funtion(10));
copy(_list.begin(), _list.end(), ostream_iterator<int>(cout, " "));
return 0;
}
Implementation of the results
analyzed
for_each(_list.begin(), _list.end(), funtion(10));
every element plus 10
, this expression funtion(10)
give birth to a funtion
thing, and is 10
used as the initial value, funtion
the constructor will save the value in a member value
of. In the for_each()
inside of, for _list
each element of the call ()
, in fact, passed on the funtion
temporary object to call operator()
, and with the container element as a parameter. Functor ( funtion
object plus each element 10
process).
Example 3
#include<iostream>
#include<list>
#include<algorithm>
#include <iterator>
using namespace std;
typedef list<int> List;
class funtion {
private:
int value;
public:
funtion(int v):value(v){ }
void operator()(int& elem)const {
elem += value;
cout << "elem" << elem << endl;
}
};
int main()
{
List _list;
for (int i = 0; i <= 10; i++)
_list.push_back(i);
funtion a1(10);
funtion a2(20);
for_each(_list.begin(), _list.end(), a1);
copy(_list.begin(), _list.end(), ostream_iterator<int>(cout, " "));
cout << endl;
for_each(_list.begin(), _list.end(), a2);
copy(_list.begin(), _list.end(), ostream_iterator<int>(cout, " "));
return 0;
}
Implementation of the results
Analysis of
the above is realized in two different functor.
Finally, note that in order to pass functor worth embodiment does not change the state of functor, this status refers to the value of the internal parameter functor, such as:
#include<iostream>
#include<list>
#include<algorithm>
#include <iterator>
using namespace std;
typedef list<int> List;
class funtion {
private:
int value;
public:
funtion(int v):value(v){ }
int operator()() {
return value++;
}
};
int main()
{
List coll;
funtion a(1);
//generate_n<back_insert_iterator<list<int> >,int,funtion& >(back_inserter(coll),9,a);
generate_n(back_inserter(coll), 9, a);
copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
cout << endl;
generate(coll.begin(), coll.end(), a);
copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
return 0;
}
Implementation of the results
of each call when the value
value is not a state value of the last call, but to re-value, such as second call generate(coll.begin(), coll.end(), a);
this process value
value or from the 1
start.
Sometimes in order to continue from a state, can be accomplished
#include<iostream>
#include<list>
#include<algorithm>
#include <iterator>
using namespace std;
typedef list<int> List;
class funtion {
private:
int value;
public:
funtion(int v):value(v){ }
int operator()() {
return value++;
}
};
int main()
{
List coll;
funtion a(1);
generate_n<back_insert_iterator<list<int> >,int,funtion& >(back_inserter(coll),9,a);
//generate_n(back_inserter(coll), 9, a);
copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
cout << endl;
generate(coll.begin(), coll.end(), a);
copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
return 0;
}
Implementation of the results
found in the second from the 10
beginning to achieve the purpose of changing the state of the functor.
But the good news is that if you use for_each()
the function, he will return functor state, realization of the principle traverse the container elements and perform for each callback function:
#include<iostream>
#include<list>
#include<algorithm>
#include <iterator>
using namespace std;
typedef list<int> List;
class funtion {
private:
int value;
public:
funtion(int v = 0):value(v){ }
void operator()(int elem) {
value += elem;
}
int GET_VALUE() {
return value;
}
};
int main()
{
List coll;
for (int i = 0; i <= 10; i++)
coll.push_back(i);
copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
funtion a = for_each(coll.begin(),coll.end(), funtion());
cout << endl << a.GET_VALUE() << endl;
funtion ab = for_each(coll.begin(), coll.end(), funtion(100));
cout << ab.GET_VALUE() << endl;
return 0;
}
Implementation of the results
In addition imitation of pre-defined function is provided in the library, not introduced here, a little reprint, screenshots, from the blog garden bloggers: alex