|< C++ STL Iterator 3 | Main | C++ STL Iterator 5 >| Site Index | Download |


 

 

 

 

 

MODULE 31a_1

THE C++ STL ITERATOR PART 4

 

 

 

 

 

 

What do we have in this session?

  1. operator- prototype

  2. iterator, operator- program example

  3. The Iterator Template Classes

  4. The random_access_iterator_tag Template Class

  5. iterator, template class

  6. The iterator_traits Template Class

  7. iterator, template class

  8. An insert_iterator Template Class

  9. An insert_iterator Template Class Members

  10. insert_iterator, container_type program example

  11. insert_iterator, container_reference program example

  12. An insert_iterator::insert_iterator

  13. insert_iterator, insert_iterator program example

  14. An insert_iterator::operator*

  15. insert_iterator, operator* program example

  16. iterator, advance() – g++ program example

  17. insert_iterator, insert_iterator – g++ program example

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

My Training Period: xx hours

 

operator- prototype

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

Parameters

 

Parameter

Description

_Left

An iterator that serves as the minuend from which another iterator is to be subtracted in forming the difference.

_Right

An iterator that serves as the subtrahend that is to be subtracted from other iterator in forming the difference.

 

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;

}

 

Output:

 

C++ STL Iterator operator-

 

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<Elem, Tr>.

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<Elem, Tr>.

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<Elem, Tr>.

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<Elem, Tr>.

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

 

31.5  Some Of The Iterator Template Class program examples

 

The random_access_iterator_tag Template Class

struct random_access_iterator_tag : public bidirectional_iterator_tag { };

// 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;

}

 

Output:

 

C++ STL Iterator template class

 

The iterator_traits Template Class

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;
   };

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;

}

 

Output:

 

C++ STL Iterator template class

 

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

 

Parameter

Description

Container

The type of container into which elements are to be inserted by an insert_iterator.

 

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.

 

An insert_iterator Template Class Members

 

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

typedef Container container_type;

// insert_iterator, container_type

#include <iterator>

#include <list>

#include <iostream>

using namespace std;

 

int main()

{

    list<int> lst1;

    insert_iterator<list<int> >::container_type lst2 = lst1;

    inserter(lst2, lst2.end()) = 12;

    inserter(lst2, lst2.end()) = 17;

    inserter(lst2, lst2.begin()) = 24;

    inserter(lst2, lst2.begin()) = 9;

    list<int>::iterator veciter;

    cout<<"The list lst2 data: ";

    for(veciter = lst2.begin(); veciter != lst2.end(); veciter++)

        cout<<*veciter<<" ";

    cout<<endl;

    return 0;

}

 

Output:

 

C++ STL Iterator container_type

typedef typename Container::reference reference;

// 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;

}

 

Output:

 

C++ STL Iterator container_reference

 

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

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

// 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;

}

 

Output:

 

C++ STL Iterator insert_iterator

 

 

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*

insert_iterator& operator*();

// 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:

 

C++ STL Iterator operator*

// *****iteratoradvance.cpp**********

// iterator, advance()

#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;

    advance(lstpos, 5);

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

    advance(lstpos, -4);

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

    advance(lstpos, 8);

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

    return 0;

}

 

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

[bodo@bakawali ~]$ ./iteratoradvance

 

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

 

 

tenouk C++ STL tutorial

 

 

 

 

 

 

 

 

 

 

 

 

 

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 complete C & C++ Standard Library documentation that includes STL.

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

 

 

 

 

 

|< C++ STL Iterator 3 | Main | C++ STL Iterator 5 >| Site Index | Download |


C++ STL Iterators Classes:  Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7