开发者

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

开发者 https://www.devze.com 2023-02-17 22:54 出处:网络
I\'m trying to create a map inside a map: typedef map<float,mytype> inner_map; typedef map<float,inner_map> outer_map;

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: _T开发者_StackOverflow2 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?


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;
}


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.


The value_type a map is a pair and therefore it has members first and second. As with all iterators, a map iterator is a pseudo-pointer, i.e. it points to data within a collection and not copies of that data.

It is almost certain internally to contain pointers rather than references due to the fact that iterators can be re-assigned (that is what you use them for) and you cannot reassign references to refer to other objects.

Even if you have a const_iterator and the type underneath is POD, it must have a pointer to it, in case someone does this:

map< int, int > m;
m.insert( make_pair( 1, 2 );
map<int,int>::const_iterator citer = m.begin();
map<int,int>::iterator iter = m.begin();
iter->second = 3;
std::cout << citer->second << '\n'; // should always print 3

The behaviour should be defined and should output 3, which would not happen if the const_iterator decided to "optimise" after all it's const and only int...


Iterators, when dereferenced, give you a reference.

0

精彩评论

暂无评论...
验证码 换一张
取 消