|< C++ STL Iterator 2 | Main | C++ STL Iterator 4 >| Site Index | Download |


 

 

 

 

 

MODULE 31a

THE C++ STL ITERATOR PART 3

 

 

 

 

 

What do we have in this page?

  1. operator!= prototypes

  2. iterator, operator!= program example

  3. operator== program example

  4. operator< prototype

  5. iterator, operator< program example

  6. operator<= program example

  7. iterator, operator> program example

  8. operator>= program example

  9. operator+ prototype

  10. iterator, operator+ program example

 

 

 

 

 

 

 

 

 

 

 

My Training Period: xx hours

 

This is a continuation from the previous Module, compiled using VC++7.0 / .Net, win32 empty console mode application. g++ program example compilation is given at the end of this Module. The source code in text for this tutorial is available in C++ STL Iterator source code.

 

The C++ STL iterators abilities that should be acquired:

 

  • To understand and appreciate the member functions program examples.

  • Able to understand and use iterator operators.

  • Able to appreciate how the class and function templates are used.

 

operator!= prototypes

  1. template<class RandomIterator>bool operator!=( const reverse_iterator<RandomIterator>& _Left,
		const reverse_iterator<RandomIterator>& _Right );
  1. 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 );
  1. 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

  • 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\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;

    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\n"

        <<"element 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<

 

 

operator<= example

// 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 vector vec data: ";

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

        cout<<*veciter<<" ";

    cout<<endl;

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

    cout<<"The iterator rvecpos1 points to the\n"

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

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

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

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

    if(rvecpos1<=rvecpos2)

        cout<<"The iterator rvecpos1 is less than or\nequal to the iterator rvecpos2."<<endl;

    else

        cout<<"The iterator rvecpos1 is greater than\nthe iterator rvecpos2."<<endl;

    cout<<"\nOperation: rvecpos2++\n";

    rvecpos2++;

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

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

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

    if(rvecpos1 <= rvecpos2)

        cout<<"The iterator rvecpos1 is less than or\nequal to the iterator rvecpos2."<<endl;

    else

        cout<<"The iterator rvecpos1 is greater than\nthe iterator rvecpos2."<<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 = 10; 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;

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

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

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

    cout<<"\nOperation: rvecpos2 > rvecpos1\n";

    if(rvecpos2 > rvecpos1)

            cout<<"The iterator rvecpos2 is greater than\nthe iterator rvecpos1."<<endl;

    else

            cout<<"The iterator rvecpos2 is not greater than\nthe iterator rvecpos1."<<endl;

    cout<<"\nOperation: rvecpos2++\n";

    rvecpos2++;

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

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

    cout<<"\nOperation: rvecpos2 > rvecpos1\n";

    if(rvecpos2 > rvecpos1)

            cout<<"The iterator rvecpos2 is greater than\nthe iterator rvecpos1."<<endl;

    else

            cout<<"The iterator rvecpos2 is not greater than\nthe iterator rvecpos1."<<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 = 10; 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;

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

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

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

    cout<<"\nOperation: rvecpos2 >= rvecpos1\n";

    if(rvecpos2 >= rvecpos1)

        cout<<"The iterator rvecpos2 is greater than or\nequal to the iterator rvecpos1."<<endl;

    else

        cout<<"The iterator rvecpos2 is not greater than\nthe iterator rvecpos1."<<endl;

    cout<<"\nOperation: rvecpos2++\n";

    rvecpos2++;

    cout<<"The iterator rvecpos2 now points to the second\nelement in the reversed sequence: "<<*rvecpos2<<endl;

    cout<<"\nOperation: rvecpos2 >= rvecpos1\n";

    if(rvecpos2 >= rvecpos1)

        cout<<"The iterator rvecpos2 is greater than\nor equal to the iterator rvecpos1."<<endl;

    else

        cout<<"The iterator rvecpos2 is not greater than\nthe iterator rvecpos1."<<endl;

    cout<<"\nOperation: rvecpos1 >= rvecpos2\n";

    if(rvecpos1 >= rvecpos2)

        cout<<"The iterator rvecpos1 is greater than\nor equal to the iterator rvecpos2."<<endl;

    else

        cout<<"The iterator rvecpos1 is not greater than\nthe iterator rvecpos2."<<endl;

    return 0;

}

 

Output:

 

C++ STL Iterator operator>=

 

operator+ prototype

 

template<class RandomIterator>
   reverse_iterator<RandomIterator> operator+( typename reverse_iterator<Iterator>::difference_type _Off,
      const reverse_iterator<RandomIterator>& _Right );

 

Parameters

 

Parameter

Description

_Off

The number of positions the const reverse_iterator is to be offset.

_Right

The const reverse_iterator that is to be offset.

 

Table 31.14

// 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 vector vec data: ";

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

        cout<<*veciter<<" ";

    cout<<endl;

    vector<int>::reverse_iterator rveciter1 = vec.rbegin();

    cout<<"\nThe iterator rveciter1 initially points to\n"

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

    cout<<"\nOperation: rveciter1 = diff + rveciter1\n";

    vector<int>::difference_type diff = 4;

    rveciter1 = diff + rveciter1;

    cout<<"The iterator rveciter1 now points to the fifth\n"

            <<"element in the reversed sequence: "<<*rveciter1<<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. 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 2 | Main | C++ STL Iterator 4 >| Site Index | Download |


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