Broad Network


Vector Methods Returning Iterators in C++

Container Library Sequences in C++ Simplified – Part 6

Forward: In this part of the series, we look at vector methods that return iterators.

By: Chrysanthus Date Published: 24 Aug 2012

Introduction

This is the part 6 of my series, Containers Library Sequences in C++, Simplified. In this part of the series, we look at vector methods that return iterators. I assume that you have read the previous parts of this series. Do not get frightened by the object type, iterator.

Do you really need to know the details of iterators before using vectors? No. However, you need to know how to use iterators with vector methods. Knowing that an iterator is a kind of pointer instantiated object is enough to appreciate vector methods. An iterator refers to the next element to be accessed in the vector list, every thing being equal.

The secret to use iterators with vector methods is this. There are certain vector methods that return iterators and others that use iterators as argument. So when a method returns an iterator, receive it in an object (identifier) in the best way you can; then use the object as argument in some other method. In that way you would have your tasks done.   

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

With my compiler, mingw, the syntax to declare an iterator is:

    __gnu_cxx::__normal_iterator<T*, vector<T, allocator<T> > > identifier;

where T is the object type of each of the elements in the vector list. The part, “__gnu_cxx::__normal_iterator” of the syntax may not work with your own compiler, so read your compiler documentation to see what to do about it. Note that in the above declaration, you have > > > with spaces and not >>> without spaces.

I begin the explanation of each method, with its syntax.

iterator begin();
This method returns an iterator that refers to the first element in the vector list. Read and try the following program (that does not display anything):

#include <iostream>
#include <vector>

using namespace std;

int main()
    {
       vector<char> myVtor(5, 2);

       myVtor[0] = 'A';   
       myVtor[1] = 'B';      
       myVtor[2] = 'C';      
       myVtor[3] = 'D';      
       myVtor[4] = 'E';   
      
       __gnu_cxx::__normal_iterator<char*, vector<char, allocator<char> > > iter = myVtor.begin();

       return 0;
    }

The right hand side of = for the iterator initialization is “myVtor.begin();”. In the initialization, the identifier for the iterator is iter. Remember, we can use the receiving iter, which has the return iterator value, as argument in some other vector methods.

const_iterator begin() const;
This is the same as the method above, but the returned value cannot be changed. Read and try the following code (that does not display anything).

#include <iostream>
#include <vector>

using namespace std;

int main()
    {
        vector<char> myVtor(5, 2);

        myVtor[0] = 'A';    
        myVtor[1] = 'B';    
        myVtor[2] = 'C';    
        myVtor[3] = 'D';    
        myVtor[4] = 'E';    
    
     const __gnu_cxx::__normal_iterator<char*, vector<char, allocator<char> > > iter = myVtor.begin();

        return 0;
    }

iterator end();
This method returns an iterator that refers to the position just after the last element in the vector list. If a new element where to be added to the list, this returned iterator would point to it. Read and try the following code (that does not display anything).

#include <iostream>
#include <vector>

using namespace std;

int main()
    {
        vector<char> myVtor(5, 2);

        myVtor[0] = 'A';    
        myVtor[1] = 'B';    
        myVtor[2] = 'C';    
        myVtor[3] = 'D';    
        myVtor[4] = 'E';    
    
     __gnu_cxx::__normal_iterator<char*, vector<char, allocator<char> > > iter = myVtor.end();

        return 0;
    }

const_iterator end() const;
Same as above, but returned value (dereferenced) cannot be changed. Try:

#include <iostream>
#include <vector>

using namespace std;

int main()
    {
        vector<char> myVtor(5, 2);

        myVtor[0] = 'A';    
        myVtor[1] = 'B';    
        myVtor[2] = 'C';    
        myVtor[3] = 'D';    
        myVtor[4] = 'E';    
    
     const __gnu_cxx::__normal_iterator<char*, vector<char, allocator<char> > > iter = myVtor.end();

        return 0;
    }

The next four methods deal with a reverse iterator. For my compiler, the reverse iterator is coded as:

reverse_iterator<__gnu_cxx::__normal_iterator<T*, vector<T, allocator<T> > > >

Note the spaces within > > > >.

reverse_iterator rbegin();
This method returns a reverse iterator that refers to the last element of the vector list. Read and try the following (which does not display anything):

#include <iostream>
#include <vector>

using namespace std;

int main()
    {
        vector<char> myVtor(5, 2);

        myVtor[0] = 'A';    
        myVtor[1] = 'B';    
        myVtor[2] = 'C';    
        myVtor[3] = 'D';    
        myVtor[4] = 'E';    
    
     reverse_iterator<__gnu_cxx::__normal_iterator<char*, vector<char, allocator<char> > > > iter = myVtor.rbegin();

        return 0;
    }

const_reverse_iterator rbegin() const;
Same as above but returned value (dereferenced) cannot be changed. Read and try the following (which does not display anything):

#include <iostream>
#include <vector>

using namespace std;

int main()
    {
        vector<char> myVtor(5, 2);

        myVtor[0] = 'A';    
        myVtor[1] = 'B';    
        myVtor[2] = 'C';    
        myVtor[3] = 'D';    
        myVtor[4] = 'E';    
    
     const reverse_iterator<__gnu_cxx::__normal_iterator<char*, vector<char, allocator<char> > > > iter = myVtor.rbegin();

        return 0;
    }

reverse_iterator rend();
This method returns a reverse iterator that refers immediately ahead of the first element of the vector list. Try:

#include <iostream>
#include <vector>

using namespace std;

int main()
    {
        vector<char> myVtor(5, 2);

        myVtor[0] = 'A';    
        myVtor[1] = 'B';    
        myVtor[2] = 'C';    
        myVtor[3] = 'D';    
        myVtor[4] = 'E';    
    
     reverse_iterator<__gnu_cxx::__normal_iterator<char*, vector<char, allocator<char> > > > iter = myVtor.rend();

        return 0;
    }

const_reverse_iterator rend() const;
Same as above but returned value (dereferenced) cannot be changed. Try:

#include <iostream>
#include <vector>

using namespace std;

int main()
    {
        vector<char> myVtor(5, 2);

        myVtor[0] = 'A';    
        myVtor[1] = 'B';    
        myVtor[2] = 'C';    
        myVtor[3] = 'D';    
        myVtor[4] = 'E';    
    
     const reverse_iterator<__gnu_cxx::__normal_iterator<char*, vector<char, allocator<char> > > > iter = myVtor.rend();

        return 0;
    }

Well, let us leave it at that for this article. In the next part of the series, we shall see how to use the returned iterator as argument for a vector method.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message