I thought that you could use a const reference in ranged-based-for-loops in C++11, But when I compile this code using g++:
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
int main() {
std::vector<std::unordered_map<std::string, int> > coordinates {
{{"x", 50}, {"y", 50}},
{{"x", 25}, {"y", 75}},
{{"x", 326}, {"y", 412}},
};
for(const auto& i : coordinates) {
std::cout << "{\"x\" : " << i["x"] << ", \"y\" : " << i["y"] << "}\n";
}
}
I get this error:
const_error.cc:13:38: error: no viable overloaded operator[] for type 'const
std::__1::unordered_map<std::__1::basic_string<char>, int,
std::__1::hash<std::__1::basic_string<char> >,
std::__1::equal_to<std::__1::basic_string<char> >,
std::__1::allocator<std::__1::pair<const std::__1::basic_string<char>,
int> > >'
std::cout << "{\"x\" : " << i["x"] << ", \"y\" : " << i["y"] << "}\n";
~^~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/unordered_map:1131:18: note:
candidate function not viable: 'this' argument has type 'const
std::__1::unordered_map<std::__1::basic_string<char>, int,
std::__1::hash<std::__1::basic_string<char> >,
std::__1::equal_to<std::__1::basic_string<char> >,
std::__1::allocator<std::__1::pair<const std::__1::basic_string<char>,
int> > >', but method is not marked const
mapped_type& operator[](const key_type& __k);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/unordered_map:1133:18: note:
candidate function not viable: 'this' argument has type 'const
std::__1::unordered_map<std::__1::basic_string<char>, int,
std::__1::hash<std::__1::basic_string<char> >,
std::__1::equal_to<std::__1::basic_string<char> >,
std::__1::allocator<std::__1::pair<const std::__1::basic_string<char>,
int> > >', but method is not marked const
mapped_type& operator[](key_type&& __k);
^
const_error.cc:13:64: error: no viable overloaded operator[] for type 'const
std::__1::unordered_map<std::__1::basic_string<char>, int,
std::__1::hash<std::__1::basic_string<char> >,
std::__1::equal_to<std::__1::basic_string<char> >,
std::__1::allocator<std::__1::pair<const std::__1::basic_string<char>,
int> > >'
std::cout << "{\"x\" : " << i["x"] << ", \"y\" : " << i["y"] << "}\n";
~^~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/unordered_map:1131:18: note:
candidate function not viable: 'this' argument has type 'const
std::__1::unordered_map<std::__1::basic_string<char>, int,
std::__1::hash<std::__1::basic_string<char> >,
std::__1::equal_to<std::__1::basic_string<char> >,
std::__1::allocator<std::__1::pair<const std::__1::basic_string<char>,
int> > >', but method is not marked const
mapped_type& operator[](const key_type& __k);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/unordered_map:1133:18: note:
candidate function not viable: 'this' argument has type 'const
std::__1::unordered_map<std::__1::basic_string<char>, int,
std::__1::hash<std::__1::basic_string<char> >,
std::__1::equal_to<std::__1::basic_string<char> >,
std::__1::allocator<std::__1::pair<const std::__1::basic_string<char>,
int> > >', but method is not marked const
mapped_type& operator[](key_type&& __k);
^
But when I remove the const
from the range-based for loop, It works just fine. Why won't my code compile fine with the const reference?
operator[]
isn't const. If the key doesn't, exist it adds a value object to that key.
Use unordered_map::at
instead.