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


 

 

 

 

MODULE 31_1

THE C++ STL ITERATOR PART 2

 

 

 

 

What do we have in this session?

  1. back_inserter() prototype

  2. iterator, back_inserter() example

  3. distance() prototype

  4. iterator, distance() example

  5. front_inserter() prototype

  6. iterator, front_inserter() example

  7. inserter() prototype

  8. operator!= prototypes

  9. iterator, operator== example

  10. operator< prototype

  11. iterator, operator< example

 

 

 

 

 

 

 

 

 

 

 

 

 

My Training Period: xx hours

 

back_inserter() prototype

 

template<class Container>
   back_insert_iterator<Container> back_inserter( Container& _Cont );

 

Parameter

 

Parameter

Description

 _Cont

The container into which the back insertion is to be executed.

 

Table 31.7

  • The return value is a back_insert_iterator associated with the container object _Cont.

  • Within the Standard Template Library, the argument must refer to one of the three sequence containers that have the member function push_back(): deque class, list class, or vector class.

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

 

C++ STL Iterator back_inserter()

 

 

distance() prototype

 

template<class InputIterator>
   typename iterator_traits<InputIterator>::difference_type
      distance( InputIterator _First, InputIterator _Last );

 

Parameters

 

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

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

}

 

Output

 

C++ STL Iterator distance()

 

front_inserter() prototype

 

template<class Container>front_insert_iterator<Container> front_inserter( Container& _Cont );

 

Parameter

 

Parameter

Description

 _Cont

The container object whose front is having an element inserted.

 

Table 31.9

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

}

 

Output:

 

C++ STL Iterator front_inserter()

 

inserter() prototype

 

template<class Container, class Iterator>
   insert_iterator<Container> inserter( Container& _Cont, Iterator _It );

 

Parameters

 

Parameter

Description

_Cont

The container to which new elements are to be added.

_It

An iterator locating the point of insertion.

 

Table 31.10

  • The return value is an insert iterator addressing the new element inserted.

  • Within the STL, the sequence and sorted associative containers may be used as a container object _Cont with the templatized inserter.

 

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

}

 

Output:

 

C++ STL Iterator inserter()

 

Operators

 

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

 

operator!= prototypes

  1. template<class RandomIterator>bool operator!=( const reverse_iterator<RandomIterator>& _Left, const reverse_iterator<RandomIterator>& _Right );

  2. 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 );

  3. template<class CharType, class Tr>bool operator!=( const istreambuf_iterator<CharType, Traits>& _Left, const istreambuf_iterator<CharType, Traits>& _Right );

 

Parameters

 

Parameter

Description

_Left

An object of type iterator.

_Right

An object of type iterator.

 

Table 31.12

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

}

 

Output:

 

C++ STL Iterator operator !=

 

operator== example

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

}

 

Output:

 

C++ STL Iterator operator==

 

operator< prototype

 

template<class RandomIterator>
   bool operator<( const reverse_iterator<RandomIterator>& _Left, const reverse_iterator<RandomIterator>& _Right );

 

Parameters

 

Parameter

Description

_Left

An object of type iterator.

_Right

An object of type iterator.

 

Table 31.13

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

}

 

Output:

 

C++ STL Iterator operator<

 

 

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

  3. C++ Templates programming tutorials.

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

 

 

 

 

 

 

 

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


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