I was puzzled when I received an error that suggested I use a .
operator to access the value in a pair from a map because when I changed it in two places in code I received a new error that suggested that I use a ->
in the second place in code. I listened to the naggy compiler. Why did I need to?
Here's what I was doing:
In a range based for loop, I want the value from the key-value pair which could be exampled:
std::map<std::string, aclass> mapthings;
...
for (auto& it : mapthings) {
fout << it.second.stringify();
}
I'm also using the same mapthings
but using the find()
function:
return (mapthings.find(name))->second;
In this code snippet
it
is of type value_type ofstd::map<std::string, aclass>
that corresponds to typeSo to access members of an object of this type you have to use operator .
In this code snippet
method
find
returns iterator that points to the target record of the map or to iterator returned byend()
. Iterators are like pointers. So you need to use operator -> to access members of the pointed object.Take into account that you could write simpler
or