Map Methods Returning Iterators in C++
Associative Container in C++ Simplified – Part 3
Forward: In this part of the series, we look at map methods that return iterators.
By: Chrysanthus Date Published: 24 Aug 2012
Introduction
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.
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> > >
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;
}
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
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT