I have a std::unordered_map
std::unordered_map<std::string, std::string> myMap;
I want to get a const iterator using find. In c++03 I would do
std::unordered_map<std::string, std::string>::const_iterator = myMap.find("SomeValue");
In c++11 I would want to use auto instead to cut down on the templates
auto = myMap.find("SomeValue");
Will this be a const_iterator or iterator? How does the compiler decide which to use? Is there a way I can force it to choose const?
nonConstMap.find
always returns aniterator
. The return type and what you do with the result (e.g. pass it to aconst_iterator
constructor) does not affect which overload is chosen. That is, it only returns aconst_iterator
if you callconstMap.find
. – Tamarah