Map Methods Returning Iterators in C++

Sep 18th, 2010 by Chrys

In this part of the series, we look at map methods that return iterators.

Associative Container in C++ Simplified - Part 3
Division 1

Introduction
This is part 3 of my series, Associative Container in C++ Simplified. In this part of the series, we look at map methods that return iterators. I assume that you have read all the prerequisites.

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.

The syntax to declare an iterator is:

        _Rb_tree_iterator<pair <const key* const, T> > iter;

Here, key stands for the key data type. T stands for the object type of the element values in the list. Note that the closing angle brackets (> >) are separated by a space.

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

#include <iostream>
#include <map>

using namespace std;

int main()
    {
        map<const char*, char> myMap;

        myMap["one"] = 'A';   
        myMap["two"] = 'B';     
        myMap["three"] = 'C';     
        myMap["four"] = 'D';     
        myMap["five"] = 'E';   
     
       _Rb_tree_iterator<pair <const char* const, char> > iter = myMap.begin();

        return 0;
    }

The right hand side of = for the iterator initialization is "myMap.begin();". In the initialization, the identifier for the iterator is iter. Note: we can use the receiving iter, which is the return iterator, as argument in some other map 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 <map>

using namespace std;

int main()
    {
        map<const char*, char> myMap;

        myMap["one"] = 'A';   
        myMap["two"] = 'B';     
        myMap["three"] = 'C';     
        myMap["four"] = 'D';     
        myMap["five"] = 'E';
     
       const _Rb_tree_iterator<pair <const char* const, char> > iter = myMap.begin();

        return 0;
    }

Note the use of the reserved word, const, before the iterator initialization statement.

iterator end();
This method returns an iterator that refers to the position just after the last element in the map 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 <map>

using namespace std;

int main()
    {
        map<const char*, int> myMap;

        myMap["one"] = 10;   
        myMap["two"] = 20;     
        myMap["three"] = 30;     
        myMap["four"] = 40;     
        myMap["five"] = 50;   
     
       _Rb_tree_iterator<pair <const char* const, int> > iter = myMap.end();

        return 0;
    }

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

#include <iostream>
#include <map>

using namespace std;

int main()
    {
        map<const char*, int> myMap;

        myMap["one"] = 10;   
        myMap["two"] = 20;     
        myMap["three"] = 30;     
        myMap["four"] = 40;     
        myMap["five"] = 50;   
     
       const _Rb_tree_iterator<pair <const char* const, int> > iter = myMap.end();

        return 0;
    }

The next four methods deal with a reverse iterator. The reverse iterator is coded as:

       reverse_iterator<_Rb_tree_iterator<pair <const key* const, T> > >

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

#include <iostream>
#include <map>

using namespace std;

int main()
    {
        map<const char*, int> myMap;

        myMap["one"] = 10;   
        myMap["two"] = 20;     
        myMap["three"] = 30;     
        myMap["four"] = 40;     
        myMap["five"] = 50;   
     
       reverse_iterator<_Rb_tree_iterator<pair <const char* const, int> > > iter = myMap.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 <map>

using namespace std;

int main()
    {
        map<const char*, int> myMap;

        myMap["one"] = 10;   
        myMap["two"] = 20;     
        myMap["three"] = 30;     
        myMap["four"] = 40;     
        myMap["five"] = 50;   
     
       const reverse_iterator<_Rb_tree_iterator<pair <const char* const, int> > > iter = myMap.rbegin();

        return 0;
    }

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

#include <iostream>
#include <map>

using namespace std;

int main()
    {
        map<const char*, int> myMap;

        myMap["one"] = 10;   
        myMap["two"] = 20;     
        myMap["three"] = 30;     
        myMap["four"] = 40;     
        myMap["five"] = 50;   
     
       reverse_iterator<_Rb_tree_iterator<pair <const char* const, int> > > iter = myMap.rend();

        return 0;
    }

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

#include <iostream>
#include <map>

using namespace std;

int main()
    {
        map<const char*, int> myMap;

        myMap["one"] = 10;   
        myMap["two"] = 20;     
        myMap["three"] = 30;     
        myMap["four"] = 40;     
        myMap["five"] = 50;   
     
       const reverse_iterator<_Rb_tree_iterator<pair <const char* const, int> > > iter = myMap.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 map methods.

Chrys

To arrive at any of the parts of this series, just type the corresponding title below and my name, Chrys, in the Search Box of this page and click Search (use menu if available):

Map Associative Container in C++
Map Element Access in C++
Map Methods Returning Iterators in C++
Map Modifiers in C++
Map Capacity in C++
Map Operations in C++
Differences between Associative Containers in C++
 

Chrys

Written by Chrys

Rate this Article:

Be the first to rate me.

Add new comment

* You must be logged in order to leave comments, please Sign in or join us.

Comments

No comments yet, be the first to comment on this article.

Chrys has 531 articles online

Related Content