Does std::map::iterator return a copy of value or a value itself?

Morse picture Morse · Mar 21, 2011 · Viewed 24.8k times · Source

I'm trying to create a map inside a map:

typedef map<float,mytype> inner_map;
typedef map<float,inner_map> outer_map;

Will I be able to put something inside inner map, or does iterator::second returns a copy?

stl_pair.h suggests the latter:

74: _T2 second;          ///< @c second is a copy of the second object

but my test program run fine with the code like this:

it = my_map.lower_bound(3.1415);
(*it).second.insert(inner_map::value_type(2.71828,"Hello world!");

So where is the truth? Is this a copy or not?

Answer

Matt Gallagher picture Matt Gallagher · Dec 18, 2012

I want to add a follow up answer to this question for people using C++11 iterators...

The following code:

std::map<std::string, std::string> m({{"a","b"},{"c","d"}});
for (auto i : m)
{
    std::cout << i.first << ": " << i.second << std::endl;
}

does copy the key and value since "auto" is a value by default, not a const reference (at least that's how it behaves clang 3.1).

Additionally, the code:

std::map<std::string, std::string> m({{"a","b"},{"c","d"}});
for (const std::pair<std::string,std:string>& i : m)
{
    std::cout << i.first << ": " << i.second << std::endl;
}

also copies the key and value since the correct code should be:

std::map<std::string, std::string> m({{"a","b"},{"c","d"}});
for (const auto& i : m)
{
    std::cout << i.first << ": " << i.second << std::endl;
}

or

std::map<std::string, std::string> m({{"a","b"},{"c","d"}});
for (const std::pair<const std::string,std:string>& i : m)
{
    std::cout << i.first << ": " << i.second << std::endl;
}