c ++ STL_5 functor

Called copy function, a is the definition operator()of the object.
Functor of three advantages:

  1. Functor can have member functions and member variables. As is usually implemented in a class, you can add other members.
  2. Each functor has its own type. Templated can be achieved, namely to achieve multiple states.
  3. 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
Here Insert Picture Description
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
Here Insert Picture Description
analyzed
for_each(_list.begin(), _list.end(), funtion(10)); every element plus 10, this expression funtion(10)give birth to a funtionthing, and is 10used as the initial value, funtionthe constructor will save the value in a member valueof. In the for_each()inside of, for _listeach element of the call (), in fact, passed on the funtiontemporary object to call operator(), and with the container element as a parameter. Functor ( funtionobject plus each element 10process).

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
Here Insert Picture DescriptionAnalysis 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
Here Insert Picture Description
of each call when the valuevalue 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 valuevalue or from the 1start.

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
Here Insert Picture Description
found in the second from the 10beginning 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
Here Insert Picture Description


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

Here Insert Picture Description
Here Insert Picture Description

Published 222 original articles · won praise 48 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_44116998/article/details/104579393