Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 358
Morse Avatar asked Mar 21 '11 12:03

Morse


People also ask

What is map iterator return?

C++ Iterators Map Iterator An iterator to the first element in the container. If a map object is const-qualified, the function returns a const_iterator . Otherwise, it returns an iterator .

Does std::map insert make copy?

Yes -- when you insert an item into an std::map, you pass it by value, so what it contains is a copy of what you passed.

What does map Find return if nothing is found C++?

Return Value: The function returns an iterator or a constant iterator which refers to the position where the key is present in the map. If the key is not present in the map container, it returns an iterator or a constant iterator which refers to map. end().

What is std::map used for?

std:: map stores unique keys only in sorted order based on chosen sorting criteria. It's easy and faster to search for elements using the key. Only one element is attached to each key. std::map can be used as an associative array.


2 Answers

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; } 
like image 150
Matt Gallagher Avatar answered Sep 18 '22 14:09

Matt Gallagher


The comment in stl_pair.h is misleading in this specific case.

There will be no copy, since the map::iterator actually refers to the original data inside the map (the value_type, which itself is a pair), it’s not a copy. Thus iterator::second also refers to the original data.

like image 20
Konrad Rudolph Avatar answered Sep 19 '22 14:09

Konrad Rudolph