| My Training Period: xx hours
back_inserter() prototype
template<class Container> back_insert_iterator<Container> back_inserter( Container& _Cont );
Parameter
// iterator, back_inserter() #include <iterator> #include <vector> #include <iostream> using namespace std;
int main() { int i; vector<int> vec; for(i = 1; i < 5; ++i) vec.push_back(i); vector <int>::iterator vecIter; cout<<"The vector vec data: "; for(vecIter = vec.begin(); vecIter != vec.end(); vecIter++) cout<<*vecIter<<" "; cout<<endl; // insertions using template function back_insert_iterator<vector<int> > backiter(vec); *backiter = 11; backiter++; *backiter = 9; backiter++; *backiter = 27; cout<<"\nOperation: *backiter = 11 then backiter++...\n"; cout<<"New vector vec data: "; for(vecIter = vec.begin(); vecIter != vec.end(); vecIter++) cout<<*vecIter<<" "; cout<<endl; cout<<"\nOperation: back_inserter(vec) = 21...\n"; // alternatively, insertions using the back_inserter() member function back_inserter(vec) = 21; back_inserter(vec) = 17; back_inserter(vec) = 33; cout<<"New vector vec data: "; for(vecIter = vec.begin(); vecIter != vec.end(); vecIter++) cout<<*vecIter<<" "; cout<<endl; return 0; }
Output:
| ||||||
template<class InputIterator> typename iterator_traits<InputIterator>::difference_typedistance( InputIterator _First, InputIterator _Last );
Parameter | Description |
_First | The first iterator whose distance from the second is to be determined. |
_Last | The second iterator whose distance from the first is to be determined. |
Table 31.8 | |
The return value is the number of times that _First must be incremented until it equal _Last.
The advance function has constant complexity when InputIterator satisfies the requirements for a random-access iterator; otherwise, it has linear complexity and so is potentially expensive.
// iterator, distance()
#include <iterator>
#include <list>
#include <iostream>
using namespace std;
int main()
{
int i;
list<int> lst;
for(i = -1; i < 10; ++i)
lst.push_back(2*i);
list <int>::iterator lstiter, lstpos = lst.begin();
cout<<"The list lst data: ";
for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)
cout<<*lstiter<<" ";
cout<<endl;
cout<<"\nOperation: lstpos = lst.begin()\n";
cout<<"The first element pointed by iterator lstpos: "<<*lstpos<<endl;
cout<<"\nOperation: advance(lstpos, 5)\n";
advance(lstpos, 5);
cout<<"lstpos is advanced 5 steps forward to point to: "<<*lstpos<<endl;
list<int>::difference_type lstdiff;
cout<<"\nOperation: lstdiff = distance(lst.begin(), lstpos)\n";
lstdiff = distance(lst.begin(), lstpos);
cout<<"The distance from lst.begin() to lstpos is: "<<lstdiff<<" elements"<<endl;
return 0;
}

template<class Container>front_insert_iterator<Container> front_inserter( Container& _Cont );
Parameter | Description |
_Cont | The container object whose front is having an element inserted. |
Table 31.9 | |
The return value is a front_insert_iterator() associated with the container object _Cont.
The member function front_insert_iterator() of the front_insert_iterator class may also be used.
Within the STL, the argument must refer to one of the two sequence containers that have the member function push_back():deque class or list class.
// iterator, front_inserter()
#include <iterator>
#include <list>
#include <iostream>
using namespace std;
int main()
{
int i;
list<int>::iterator lstiter;
list<int> lst;
for(i = -2; i<=5; ++i)
lst.push_back(i);
cout<<"The list lst data: ";
for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)
cout<<*lstiter<<" ";
cout<<endl;
// using the template function to insert an element
cout<<"\nOperation: ++Iter then *Iter = 12...\n";
front_insert_iterator< list < int> > Iter(lst);
*Iter = 21;
++Iter;
*Iter = 12;
cout<<"New list lst data: ";
for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)
cout<<*lstiter<<" ";
cout<<endl;
cout<<"\nOperation: front_inserter(lst) = 23...\n";
// alternatively, using the front_insert() member function
front_inserter(lst) = 23;
front_inserter(lst) = 9;
cout<<"New list lst data: ";
for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)
cout<<*lstiter<<" ";
cout<<endl;
return 0;
}

inserter() prototype
template<class Container, class Iterator>insert_iterator<Container> inserter( Container& _Cont, Iterator _It );
Parameters
| |||||||||
// iterator, inserter()
#include <iterator>
#include <list>
#include <iostream>
using namespace std;
int main()
{
int i;
list <int>::iterator lstiter;
list<int> lst;
for(i = -3; i<=2; ++i)
lst.push_back(i);
cout<<"The list lst data: ";
for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)
cout<<*lstiter<<" ";
cout<<endl;
// using the template version to insert an element
insert_iterator<list <int> > Iter(lst, lst.begin());
*Iter = 7;
++Iter;
*Iter = 12;
cout<<"\nOperation: *Iter = 7 then ++Iter...\n";
cout<<"After the insertions, the list lst data: \n";
for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)
cout<<*lstiter<<" ";
cout<<endl;
// alternatively, using the member function inserter() to insert an element
inserter(lst, lst.end()) = 31;
inserter(lst, lst.end()) = 42;
cout<<"\nOperation: inserter(lst, lst.end()) = 42...\n";
cout<<"After the insertions, the list lst data: \n";
for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)
cout<<*lstiter<<" ";
cout<<endl;
return 0;
}

Operator | Description |
operator!= | Tests if the iterator object on the left side of the operator is not equal to the iterator object on the right side. |
operator== | Tests if the iterator object on the left side of the operator is equal to the iterator object on the right side. |
operator< | Tests if the iterator object on the left side of the operator is less than the iterator object on the right side. |
operator<= | Tests if the iterator object on the left side of the operator is less than or equal to the iterator object on the right side. |
operator> | Tests if the iterator object on the left side of the operator is greater than the iterator object on the right side. |
operator>= | Tests if the iterator object on the left side of the operator is greater than or equal to the iterator object on the right side. |
operator+ | Adds an offset to an iterator and returns the new reverse_iterator addressing the inserted element at the new offset position. |
operator- | Subtracts one iterator from another and returns the difference. |
Table 31.11 | |
template<class RandomIterator>bool operator!=( const reverse_iterator<RandomIterator>& _Left, const reverse_iterator<RandomIterator>& _Right );
template<class Type, class CharType, class Traits, class Distance>bool operator!=( const istream_iterator<Type, CharType, Traits, Distance>& _Left, const istream_iterator<Type, CharType, Traits, Distance>& _Right );
template<class CharType, class Tr>bool operator!=( const istreambuf_iterator<CharType, Traits>& _Left, const istreambuf_iterator<CharType, Traits>& _Right );
Parameter | Description |
_Left | An object of type iterator. |
_Right | An object of type iterator. |
Table 31.12 | |
The return value is true if the iterator objects are not equal; false if the iterator objects are equal.
One iterator object is equal to another if they address the same elements in a container. If two iterators point to different elements in a container, then they are not equal.
// iterator, operator!=
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
int i;
vector<int> vec;
for(i = 1; i<=10; ++i)
vec.push_back(i);
vector<int>::iterator veciter;
cout<<"The vector vec data: ";
for(veciter = vec.begin(); veciter != vec.end(); veciter++)
cout<<*veciter<<" ";
cout<<endl;
// initializing reverse_iterators to the last element
vector<int>::reverse_iterator rvecpos1 = vec.rbegin(), rvecpos2 = vec.rbegin();
cout<<"The iterators rvecpos1 and rvecpos2 points to the first\n"
<<"element in the reversed sequence: "<<*rvecpos1<<endl;
cout<<"\nOperation: rvecpos1 != rvecpos2\n";
if(rvecpos1 != rvecpos2)
cout<<"The iterators are not equal."<<endl;
else
cout<<"The iterators are equal."<<endl;
rvecpos1++;
cout<<"\nThe iterator rvecpos1 now points to the second\nelement in the reversed sequence: "<<*rvecpos1<<endl;
cout<<"\nOperation: rvecpos1 != rvecpos2\n";
if(rvecpos1 != rvecpos2)
cout<<"The iterators are not equal."<<endl;
else
cout<<"The iterators are equal."<<endl;
return 0;
}

The return value is true if the iterator objects are equal; false if the iterator objects are not equal.
One iterator object is equal to another if they address the same elements in a container. If two iterators point to different elements in a container, then they are not equal.
// iterator, operator==
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
int i;
vector<int> vec;
for(i = 11; i<15; ++i)
vec.push_back(i);
vector <int>::iterator veciter;
cout<<"The vector vec data: ";
for(veciter = vec.begin(); veciter != vec.end(); veciter++)
cout<<*veciter<<" ";
cout<<endl;
// initializing reverse_iterators to the last element
vector<int>::reverse_iterator rvecpos1 = vec.rbegin(), rvecpos2 = vec.rbegin();
cout<<"\nThe iterators rvecpos1 and rvecpos2 points\nto the first"
<<"element in the reversed sequence: "<<*rvecpos1<<endl;
cout<<"\nOperation: rvecpos1 == rvecpos2\n";
if(rvecpos1 == rvecpos2)
cout<<"The iterators are equal."<<endl;
else
cout<<"The iterators are not equal."<<endl;
rvecpos1++;
cout<<"\nThe iterator rvecpos1 now points to the second\n"
<<"element in the reversed sequence: "<<*rvecpos1<<endl;
cout<<"\nOperation: rvecpos1 == rvecpos2\n";
if(rvecpos1 == rvecpos2)
cout<<"The iterators are equal."<<endl;
else
cout<<"The iterators are not equal."<<endl;
return 0;
}

template<class RandomIterator>bool operator<( const reverse_iterator<RandomIterator>& _Left, const reverse_iterator<RandomIterator>& _Right );
Parameter | Description |
_Left | An object of type iterator. |
_Right | An object of type iterator. |
Table 31.13 | |
The return value is true if the iterator on the left side of the expression is less than the iterator on the right side of the expression; false if it is greater than or equal to the iterator on the right.
One iterator object is less than another if it addresses an element that occurs earlier in the container than the element addressed by the other iterator object.
One iterator object is not less than another if it addresses either the same element as the other iterator object or an element that occurs later in the container than the element addressed by the other iterator object.
// iterator, operator<
#include <iterator>
#include <vector>
#include <iostream>
int main()
{
using namespace std;
int i;
vector<int> vec;
for(i = 10; i<= 17; ++i)
vec.push_back(i);
vector<int>::iterator veciter;
cout<<"The initial vector vec is: ";
for(veciter = vec.begin(); veciter != vec.end(); veciter++)
cout<<*veciter<<" ";
cout<<endl;
// initializing reverse_iterators to the last element
vector<int>::reverse_iterator rvecpos1 = vec.rbegin(), rvecpos2 = vec.rbegin();
cout<<"The iterators rvecpos1 & rvecpos2 initially point\nto the first element in the reversed sequence: "<<*rvecpos1<<endl;
cout<<"\nOperation: rvecpos1 < rvecpos2\n";
if(rvecpos1 < rvecpos2)
cout<<"The iterator rvecpos1 is less than the iterator rvecpos2."<<endl;
else
cout<<"The iterator rvecpos1 is not less than the iterator rvecpos2."<<endl;
cout<<"\nOperation: rvecpos1 > rvecpos2\n";
if(rvecpos1 > rvecpos2)
cout<<"The iterator rvecpos1 is greater than the iterator rvecpos2."<<endl;
else
cout<<"The iterator rvecpos1 is not greater than the iterator rvecpos2."<<endl;
cout<<"\nOperation: rvecpos2++;\n";
rvecpos2++;
cout<<"The iterator rvecpos2 now points to the second\nelement in the reversed sequence: "<<*rvecpos2<<endl;
cout<<"\nOperation: rvecpos1 < rvecpos2\n";
if(rvecpos1 < rvecpos2)
cout<<"The iterator rvecpos1 is less than the iterator rvecpos2."<<endl;
else
cout<<"The iterator rvecpos1 is not less than the iterator rvecpos2."<<endl;
cout<<"\nOperation: rvecpos1 > rvecpos2\n";
if(rvecpos1 > rvecpos2)
cout<<"The iterator rvecpos1 is greater than the iterator rvecpos2."<<endl;
else
cout<<"The iterator rvecpos1 is not greater than the iterator rvecpos2."<<endl;
return 0;
}

The source code in text for this tutorial is available inC++ STL Iterator source code.
Acomplete C++ Standard Library documentation that includes STL.