C ++ - iterator

In addition to the definition of each container iterator, iterator library defines additional iterator.

Iterator 1. Insert: insert elements into the container

1.1 back_inserter

1.2 front_inserter

1.3 inserter

= IT * val;
 // vertical equivalent 
IT c.insert = (it, val); // before val inserting it, and to it 
++ IT; // point to the original element
list<int> lst={1,2,3,4};
list<int> lst2,lst3;

copy(list.cbegin(),last.cend(),front_inserter(lst2));//lst2=4,3,2,1
copy(list.cbegin(),last.cend(),inserter(lst3));//lst3=1,2,3,4

 

2. Flow iterator: bound to the input and output streams, for traversing

stream_iterator<int> in_iter(cin),eof;
vector<int> vec(in_iter,eof);

 

3. Reverse iterator: move backward (forward_list not, there is no flow iterator)

Sort (vec.begin (), vec.end ()); // normal order 
Sort (vec.end (), vec.begin ()); // reverse order

 

4. Move the iterator

 

Guess you like

Origin www.cnblogs.com/yrm1160029237/p/11518370.html