# operator- prototype

template<class RandomIterator>typename reverse_iterator<RandomIterator>::difference_type operator-( const reverse_iterator<RandomIterator>& _Left, const reverse_iterator<RandomIterator>& _Right );

# Parameters

Parameter

_Left

_Right

#### Table 31.15

• The return value is the difference between two iterators: _Left - _Right.  The difference equals the minuend minus the subtrahend.

// iterator, operator-

#include <iterator>

#include <vector>

#include <iostream>

using namespace std;

int main()

{

int i;

vector<int> vec;

for(i = 10; i<=15; ++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;

cout<<"\nOperation: rvecpos1 = vec.rbegin() and rvecpos2 = vec.rbegin()\n";

vector<int>::reverse_iterator rvecpos1 = vec.rbegin(), rvecpos2 = vec.rbegin();

cout<<"The iterators rvecpos1 & rvecpos2 initially point to\n"

<<"the first element in the reversed sequence: "<<*rvecpos1<<endl;

cout<<"\nOperation: for(i = 1; i<=5; ++i) and rvecpos2++\n";

for(i = 1; i<=5; ++i)

rvecpos2++;

cout<<"The iterator rvecpos2 now points to the sixth\n"

<<"element in the reversed sequence: "<<*rvecpos2<<endl;

cout<<"\nOperation: diff = rvecpos2 - rvecpos1\n";

vector<int>::difference_type diff = rvecpos2 - rvecpos1;

cout<<"The iterators difference: rvecpos2 - rvecpos1= "<<diff<<endl;

return 0;

}

# The Iterator Template Classes

 Class Description back_insert_iterator The template class describes an output iterator object. It inserts elements into a container of type Container, which it accesses through the protected pointer object it stores called container. bidirectional_iterator_tag A class that provides a return type for an iterator_category() function that represents a bidirectional iterator. front_insert_iterator The template class describes an output iterator object. It inserts elements into a container of type Container, which it accesses through the protected pointer object it stores called container. forward_iterator_tag A class that provides a return type for an iterator_category() function that represents a forward iterator. input_iterator_tag A class that provides a return type for an iterator_category() function that represents a bidirectional iterator. insert_iterator The template class describes an output iterator object. It inserts elements into a container of type Container, which it accesses through the protected pointer object it stores called container. It also stores the protected iterator object, of class Container::iterator, called iter. istream_iterator The template class describes an input iterator object. It extracts objects of class Type from an input stream, which it accesses through an object it stores, of type pointer to basic_istream. istreambuf_iterator The template class describes an output iterator object. It inserts elements of class Elem into an output stream buffer, which it accesses through an object it stores, of type pointer to basic_streambuf. iterator The template class is used as a base type for all iterators. iterator_traits A template helper class providing critical types that are associated with different iterator types so that they can be referred to in the same way. ostream_iterator The template class describes an output iterator object. It inserts objects of class Type into an output stream, which it accesses through an object it stores, of type pointer to basic_ostream. ostreambuf_iterator Class The template class describes an output iterator object. It inserts elements of class Elem into an output stream buffer, which it accesses through an object it stores, of type pointer to basic_streambuf. output_iterator_tag A class that provides a return type for iterator_category() function that represents an output iterator. random_access_iterator_tag A class that provides a return type for iterator_category() function that represents a random-access iterator. reverse_iterator The template class describes an object that behaves like a random-access iterator, only in reverse. Table 31.16

# The random_access_iterator_tag Template Class

• A class that provides a return type for iterator_category function that represents a random-access iterator.

`struct random_access_iterator_tag : public bidirectional_iterator_tag { };`
• The category tag classes are used as compile tags for algorithm selection. The template function needs to find the most specific category of its iterator argument so that it can use the most efficient algorithm at compile time. For every iterator of type Iterator, iterator_traits<Iterator>::iterator_category must be defined to be the most specific category tag that describes the iterator's behavior.

• The type is the same as iterator<Iter>::iterator_category when Iter describes an object that can serve as a random-access iterator.

// iterator, template class

#include <iterator>

#include <vector>

#include <iostream>

#include <list>

using namespace std;

int main()

{

vector<int> vec1;

vector<char> vec2;

list<char> lst;

iterator_traits<vector<int>::iterator>::iterator_category cati;

iterator_traits<vector<char>::iterator>::iterator_category catc;

iterator_traits<list<char>::iterator>::iterator_category catlst;

// both are random-access iterators

cout<<"The type of iterator for vector<int> is\nidentified by the tag:\n "<<typeid(cati).name()<<endl;

cout<<"The type of iterator for vector<char> is \nidentified by the tag:\n "<<typeid(catc).name()<<"\n";

cout<<"\nOperation: typeid(cati) == typeid(catc)\n";

if(typeid(cati) == typeid(catc))

cout<<"The iterators type are the same."<<endl<<endl;

else

cout<<"The iterators type are not the same."<<endl<<endl;

// but the list iterator is bidirectional, not random access

cout<<"The type of iterator for list<char> is\nidentified by the tag:\n"<<typeid(catlst).name()<<endl;

cout<<"\nOperation: typeid(vec1.begin())==typeid(vec2.begin())\n";

if(typeid(vec1.begin()) == typeid(vec2.begin()))

cout<<"The iterators type are the same."<<endl;

else

cout<<"The iterators type are not the same."<<endl;

return 0;

}

# The iterator_traits Template Class

• A template helper class providing critical types that are associated with different iterator types so that they can be referred to in the same way.  Note that, from the following three class templates you can see how the typedef defined in the template class as well as struct usage.

`template<class Iterator>`
`   struct iterator_traits`
`{`
`   	typedef typename Iterator::iterator_category iterator_category;`
`   	typedef typename Iterator::value_type value_type;`
`   	typedef typename Iterator::difference_type difference_type;`
`   	typedef typename Iterator::pointer pointer;`
`   	typedef typename Iterator::reference reference;`
`   };`
` `
`template<class Type>`
`   struct iterator_traits<Type*> `
`{`
`   	typedef random_access_iterator_tag iterator_category;`
`  	typedef Type value_type;`
`   	typedef ptrdiff_t difference_type;`
`   	typedef Type *pointer;`
`   	typedef Type& reference;`
`   };`
` `
`template<class Type>`
`   struct iterator_traits<const Type*>`
`{`
`   	typedef random_access_iterator_tag iterator_category;`
`   	typedef Type value_type;`
`   	typedef ptrdiff_t difference_type;`
`   	typedef const Type *pointer;`
`   	typedef const Type& reference;`
`   };`
• The template class defines the following member types:

 Type Description iterator_category A synonym for Iterator::iterator_category. value_type A synonym for Iterator::value_type. difference_type A synonym for Iterator::difference_type. pointer A synonym for Iterator::pointer. reference A synonym for Iterator::reference. Table 31.17:  Member types

// iterator, template class

#include <iostream>

#include <iterator>

#include <vector>

#include <list>

using namespace std;

template<class ite>

// create a function of template class type...

void funct(ite i1, ite i2)

{

iterator_traits<ite>::iterator_category cat;

cout<<"Test the iterator type...\n";

cout<<typeid(cat).name()<<endl;

// print the container data

cout<<"The data: ";

while(i1 != i2)

{

iterator_traits<ite>::value_type p;

p = *i1;

cout<<p<<" ";

i1++;

};

cout<<endl<<endl;

};

int main()

{

// declare containers vector and list

vector<char> vec(9, 'T');

list<int> lst(8, 7);

// function call...

funct(vec.begin(), vec.end());

funct(lst.begin(), lst.end());

return 0;

}

# An insert_iterator Template Class

• Describes an iterator adaptor that satisfies the requirements of an output iterator. It inserts, rather than overwrites, elements into a sequence and thus provides semantics that are different from the overwrite semantics provided by the iterators of the C++ sequence and associative containers.

• The insert_iterator class is templatized on the type of container being adapted.

`template <class Container>`

# Parameters

#### Table 31.18

• The container of type Container must satisfy the requirements for a variable-sized container and have a two-argument insert member function where the parameters are of type Container::iterator and Container::value_type and that returns a type Container::iterator.

• STL sequence and sorted associative containers satisfy these requirements and can be adapted to use with insert_iterators. For associative containers, the position argument is treated as a hint, which has the potential to improve or degrade performance depending on how good the hint is.

• An insert_iterator must always be initialized with its container.

# Typedefs

 Typedef Description container_type A type that represents the container into which a general insertion is to be made. reference A type that provides a reference to an element in a sequence controlled by the associated container. Table 31.19
• A type that represents the container into which a general insertion is to be made.

`typedef Container container_type;`
• The type is a synonym for the template parameter Container.

# Output:

• A type that provides a reference to an element in a sequence controlled by the associated container.

`typedef typename Container::reference reference;`
• The type describes a reference to an element of the sequence controlled by the associated container.

// insert_iterator, container_reference

#include <iterator>

#include <list>

#include <iostream>

using namespace std;

int main()

{

list<int> lst;

insert_iterator<list<int> > iivIter(lst, lst.begin());

*iivIter = 12;

*iivIter = 21;

*iivIter = 9;

*iivIter = 31;

list<int>::iterator lstIter;

cout<<"The list lst data: ";

for(lstIter = lst.begin(); lstIter != lst.end(); lstIter++)

cout<<*lstIter<<" ";

cout<<endl;

cout<<"\nOperation: refirst = *(lst.begin())\n";

insert_iterator<list<int> >::reference refirst = *(lst.begin());

cout<<"The first element in the list lst is: "<<refirst<<endl;

return 0;

}

# Member Functions

 Member function Description insert_iterator Constructs an insert_iterator that inserts an element into a specified position in a container. Table 31.20

# An insert_iterator::insert_iterator

• Constructs an insert_iterator that inserts an element into a specified position in a container.

`insert_iterator(Container& _Cont, typename Container::iterator _It);`

# Parameters

 Parameter Description _Cont The container into which the insert_iterator is to insert elements. _It The position for the insertion. Table 31.21
• All containers have the insert member function called by the insert_iterator.

• For associative containers the position parameter is merely a suggestion. The inserter function provides a convenient way to insert to values.

// insert_iterator, insert_iterator

#include <iterator>

#include <list>

#include <iostream>

int main()

{

using namespace std;

int i;

list <int>::iterator lstiter;

list<int> lst;

for(i = 10; i<15; ++i)

lst.push_back(i);

cout<<"The list lst data: ";

for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)

cout<<*lstiter<<" ";

cout<<endl;

// using the member function to insert an element

cout<<"\nOperation: inserter(lst, lst.begin()) = 21...";

inserter(lst, lst.begin()) = 21;

inserter(lst, lst.begin()) = 27;

cout<<"\nAfter the insertions, the list lst data:\n";

for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)

cout<<*lstiter<<" ";

cout<<endl;

// alternatively, using the template version

cout<<"\nOperation: Iter(lst, lst.end()) and *Iter = 9...";

insert_iterator< list < int> > Iter(lst, lst.end());

*Iter = 9;

*Iter = 33;

cout<<"\nAfter the insertions, the list lst data:\n";

for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)

cout<<*lstiter<<" ";

cout<<endl;

return 0;

}

# Operators

 Operator Description operator* Dereferencing operator used to implement the output iterator expression such as *i = x for a general insertion. operator++ Increments the insert_iterator to the next location into which a value may be stored. operator= Assignment operator used to implement the output iterator expression such as *i = x for a general insertion. Table 31.22

# An insert_iterator::operator*

• Dereferences the insert iterator returning the element is addresses.

`insert_iterator& operator*();`
• The return value is the member function returns the value of the element addressed.

• Used to implement the output iterator expression *Iter = value. If Iter is an iterator that addresses an element in a sequence, then *Iter = value replaces that element with value and does not change the total number of elements in the sequence.

// insert_iterator, operator*

#include <iterator>

#include <list>

#include <iostream>

using namespace std;

int main()

{

int i;

list<int>::iterator lstiter;

list<int> lst;

for(i = 10; i<=15; ++i)

lst.push_back(i);

cout<<"The original list lst data: ";

for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)

cout<<*lstiter<<" ";

cout<<endl;

cout<<"\nOperation: Iter(lst, lst.begin()) and *Iter = 21... \n";

insert_iterator< list < int> > Iter(lst, lst.begin());

*Iter = 21;

*Iter = 9;

*Iter = 34;

cout << "After the insertions, the list lst data:\n";

for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)

cout<<*lstiter<<" ";

cout<<endl;

return 0;

}

# Output:

• The following is program example compiled using g++.  g++ will prompt you if old STL constructs that do not comply to standard, used in your programs such as examples presented at the beginning of this Module.

#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(i);

list<int>::iterator lstIter, lstpos = lst.begin();

cout<<"The lst list data: ";

for(lstIter = lst.begin(); lstIter != lst.end(); lstIter++)

cout<<*lstIter<<" ";

cout<<endl;

cout<<"The the first element pointed by iterator lstpos is: "<<*lstpos<<endl;

cout<<"Advanced lstpos 5 steps forward pointing to the "<<*lstpos<<endl;

cout<<"Moved lstpos 4 steps backward pointing to the "<<*lstpos<<endl;

cout<<"Finally, the last element pointed by iterator lstpos is: "<<*lstpos<<endl;

return 0;

}

The lst list data: 1 2 3 4 5 6 7 8 9 10

The the first element pointed by iterator lstpos is: 1

Advanced lstpos 5 steps forward pointing to the 6

Moved lstpos 4 steps backward pointing to the 2

Finally, the last element pointed by iterator lstpos is: 10

// *******insertiterator.cpp********

// insert_iterator, insert_iterator

#include <iterator>

#include <list>

#include <iostream>

int main()

{

using namespace std;

int i;

list <int>::iterator lstiter;

list<int> lst;

for(i = 10; i<15; ++i)

lst.push_back(i);

cout<<"The list lst data: ";

for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)

cout<<*lstiter<<" ";

cout<<endl;

// using the member function to insert an element

cout<<"\nOperation: inserter(lst, lst.begin()) = 21...";

inserter(lst, lst.begin()) = 21;

inserter(lst, lst.begin()) = 27;

cout<<"\nAfter the insertions, the list lst data:\n";

for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)

cout<<*lstiter<<" ";

cout<<endl;

// alternatively, using the template version

cout<<"\nOperation: Iter(lst, lst.end()) and *Iter = 9...";

insert_iterator< list < int> > Iter(lst, lst.end());

*Iter = 9;

*Iter = 33;

cout<<"\nAfter the insertions, the list lst data:\n";

for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)

cout<<*lstiter<<" ";

cout<<endl;

return 0;

}

[bodo@bakawali ~]\$ g++ insertiterator.cpp -o insertiterator

[bodo@bakawali ~]\$ ./insertiterator

The list lst data: 10 11 12 13 14

Operation: inserter(lst, lst.begin()) = 21...

After the insertions, the list lst data:

27 21 10 11 12 13 14

Operation: Iter(lst, lst.end()) and *Iter = 9...

After the insertions, the list lst data:

27 21 10 11 12 13 14 9 33

# Further C++ STL iterators related reading:

1. The source code in text for this tutorial is available in C++ STL Iterator source code.

2. C++ Templates programming tutorials.

3. A documentation that includes STL.

4. Check the best selling C / C++ and STL books at Amazon.com.