c ++ STL_4 of iterator adapter

Here we introduce three iterator adapter:

  1. Placement type iterator
  2. Flow iterator
  3. Reverse iterator type

Placement type iterator
to solve the problem of the target container space problem, the size of the target container demand increases.

back_inserter()Placed in the end of the container, for vectot,deque,listcontainers:

#include<iostream>
#include<vector>
#include<algorithm>
#include <iterator>
using namespace std;
typedef vector<int> Vector;
typedef Vector::iterator Iterator;
int main()
{
	Vector vec,vec_1,vec_2;
	for(int i = 0;i <= 10;i++)
		vec.push_back(rand() % 10);
	cout << "生成的序列vec:" << endl;
	for (auto tp : vec)
		cout << tp << " ";
	cout << endl;
	copy(vec.begin(),vec.end(),back_inserter(vec_1));
	copy(vec_1.begin(), vec_1.end(), back_inserter(vec_2));
	cout << endl;
	cout << "生成的序列vec_1:" << endl;
	copy(vec_1.begin(), vec_1.end(), ostream_iterator<int>(cout, " "));
	cout << endl << "生成的序列vec_2:" << endl;
	copy(vec_2.begin(), vec_2.end(), ostream_iterator<int>(cout, " "));
	return 0;
}

Implementation of the results
Here Insert Picture Description
using the back_inserter()fact that internal call push_back(), add elements to the end of the container.

Examples not covered:

#include<iostream>
#include<vector>
#include<algorithm>
#include <iterator>
using namespace std;
typedef vector<int> Vector;
typedef Vector::iterator Iterator;
int main()
{
	Vector vec,vec_1,vec_2;
	for(int i = 0;i <= 10;i++)
		vec.push_back(rand() % 10);
	cout << "生成的序列vec:" << endl;
	for (auto tp : vec)
		cout << tp << " ";
	cout << endl;
	copy(vec.begin(),vec.end(),back_inserter(vec_1));
	for (int i = 0; i <= 10; i++)
		vec_2.push_back(rand() % 10);
	cout << "生成的序列vec_2:" << endl;
	copy(vec_2.begin(), vec_2.end(), ostream_iterator<int>(cout, " "));
	cout << endl;
	copy(vec_2.begin(), vec_2.end(), back_inserter(vec_1));
	cout << "生成的序列vec_1:" << endl;
	copy(vec_1.begin(), vec_1.end(), ostream_iterator<int>(cout, " "));
	return 0;
}

Implementation of the results
Here Insert Picture Description

front_inserter()Placed in the container header, for deque, list` container.

#include<iostream>
#include<list>
#include<algorithm>
#include <iterator>
using namespace std;
typedef list<int> List;
typedef List::iterator Iterator;
int main()
{
	List vec,vec_1,vec_2;
	for(int i = 0;i <= 10;i++)
		vec.push_back(rand() % 10);
	cout << "生成的序列vec:" << endl;
	for (auto tp : vec)
		cout << tp << " ";
	cout << endl;
	copy(vec.begin(),vec.end(),back_inserter(vec_1));
	for (int i = 0; i <= 10; i++)
		vec_2.push_back(rand() % 10);
	cout << "生成的序列vec_2:" << endl;
	copy(vec_2.begin(), vec_2.end(), ostream_iterator<int>(cout, " "));
	cout << endl;
	copy(vec_2.begin(), vec_2.end(), front_inserter(vec_1));
	cout << "生成的序列vec_1:" << endl;
	copy(vec_1.begin(), vec_1.end(), ostream_iterator<int>(cout, " "));
	return 0;
}

Implementation of the results
Here Insert Picture Description
Note that this operation is inserted, is inserted in the reverse order.

inserter(container,pos)Common type is inserted in the container posplacement element position, the arrangement order of the elements and consistent placement order. All the containers having this function inserter.

#include<iostream>
#include<list>
#include<algorithm>
#include <iterator>
using namespace std;
typedef list<int> List;
typedef List::iterator Iterator;
int main()
{
	List vec,vec_1,vec_2;
	for(int i = 0;i <= 10;i++)
		vec.push_back(rand() % 10);
	cout << "生成的序列vec:" << endl;
	for (auto tp : vec)
		cout << tp << " ";
	cout << endl;
	copy(vec.begin(),vec.end(),inserter(vec_1,vec_1.begin()));
	cout << "生成的序列vec_1:" << endl;
	copy(vec_1.begin(), vec_1.end(), ostream_iterator<int>(cout, " "));
	return 0;
}

Implementation of the results
Here Insert Picture Description


Flow iterator
functions as all the word from the standard input, until the end of file read.

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include <iterator>
using namespace std;
int main()
{
	vector<int> vec;
	copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(vec));
	copy(vec.begin(),vec.end(),ostream_iterator<int>(cout," "));
	return 0;
}

Implementation of the results
Here Insert Picture Description
istream_iterator<int>(cin):
function equivalent cin >>;

istream_iterator<int>():
Generating representing "end character stream" the iterator, which can not be read to mean anything.

back_inserter(vec):
In the copycase, the data (from cin) the copy into the vecvessel


Converse type iterator
i.e. reverse function

#include<iostream>
#include<vector>
#include<algorithm>
#include <iterator>
using namespace std;
typedef vector<int> Vector;
typedef Vector::iterator Iterator;
int main()
{
	Vector vec;
	for(int i = 0;i <= 10;i++)
		vec.push_back(rand() % 10);
	cout << "生成的序列vec:" << endl;
	copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, " "));
	cout << endl << "逆序后" << endl;
	copy(vec.rbegin(), vec.rend(), ostream_iterator<int>(cout, " "));
	return 0;
}

Results of
Here Insert Picture Description
which rbegin()point to the original end()position, rend()point to the original begin()position.

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

Guess you like

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