Online Book Reader

Home Category

Professional C__ - Marc Gregoire [210]

By Root 1156 0
and can be less efficient than insert().

The fact that operator[] creates a new element in the map if the requested element does not already exist means that this operator is not marked as const. This sounds obvious, but might sometimes look counter-intuitive. For example, suppose you have the following function:

void func(const map& m)

{

cout << m[1] << endl; // Error

}

Code snippet from MapBasics\MapAsParameter.cpp

This will fail to compile, even though you appear to be just reading the value m[1]. It fails because the variable m is a const reference to a map, and operator[] is not marked as const. Instead, you should use the find() method as follows:

void func(const map& m)

{

auto iter = m.find(1);

if (iter != m.end())

cout << iter->second << endl;

}

Code snippet from MapBasics\MapAsParameter.cpp

Or, if your compiler doesn’t support the C++11 auto keyword:

void func(const map& m)

{

map::const_iterator iter = m.find(1);

if (iter != m.end())

cout << iter->second << endl;

}

Code snippet from MapBasics\MapAsParameter.cpp

map Iterators

map iterators work similarly to the iterators on the sequential containers. The major difference is that the iterators refer to key/value pairs instead of just the values. In order to access the value, you must retrieve the second field of the pair object. Here is how you can iterate through the map from the previous example:

map dataMap;

dataMap[1] = Data(4);

dataMap[1] = Data(6); // Replaces the element with key 1

for (auto iter = dataMap.cbegin(); iter != dataMap.cend(); ++iter) {

cout << iter->second.getVal() << endl;

}

Code snippet from MapBasics\MapIterators.cpp

Take another look at the expression used to access the value:

iter->second.getVal()

iter refers to a key/value pair, so you can use the -> operator to access the second field of that pair, which is a Data object. You can then call the getVal() method on that data object.

Note that the following code is functionally equivalent:

(*iter).second.getVal()

You still see a lot of code like that around because the -> operator didn’t used to be implemented for iterators.

Using the C++11 range-based for loop, the loop can be written even more elegantly as follows:

for (auto& p : dataMap) {

cout << p.second.getVal() << endl;

}

Code snippet from MapBasics\MapIterators.cpp

If your compiler does not support the preceding C++11 versions, you have to write the loop as follows:

for (map::const_iterator iter = dataMap.begin();

iter != dataMap.end(); ++iter) {

cout << iter->second.getVal() << endl;

}

Code snippet from MapBasics\MapIterators.cpp

You can modify element values through non-const iterators, but the compiler will generate an error if you try to modify the key of an element, even through a non-const iterator, because it would destroy the sorted order of the elements in the map.

map iterators are bidirectional, meaning you can traverse them in both directions.

Looking Up Elements

The map provides logarithmic lookup of elements based on a supplied key. If you already know that an element with a given key is in the map, the simplest way to look it up is through operator[]. The nice thing about operator[] is that it returns a reference to the element that you can use (or modify on a non-const map) directly, without worrying about pulling the value out of a pair object. Here is an extension to the preceding example to call the setVal() method on the Data object value at key 1:

map dataMap;

dataMap[1] = Data(4);

dataMap[1] = Data(6);

dataMap[1].setVal(100);

Code snippet from MapBasics\MapLookup.cpp

However, if you don’t know whether the element exists, you may not want to use operator[], because it will insert a new element with that key if it doesn’t find one already. As an alternative, the map provides a find() method that returns an iterator referring to the element with the specified key, if it exists, or the end() iterator if it’s not in the map. Here is an example using find() to perform the

Return Main Page Previous Page Next Page

®Online Book Reader