基于范围的循环中的 const 引用不起作用
const reference in range-based-for-loop not working
我认为您可以在 C++11 中基于范围的 for 循环中使用 const 引用,但是当我使用 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";
}
}
我收到这个错误:
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);
^
但是当我从基于范围的 for 循环中删除 const
时,它工作得很好。为什么我的代码无法使用 const 引用正常编译?
map
和 unordered_map
的 []
运算符需要在非常量对象上调用,因为如果不存在,它将更新对象以插入新条目对于那把钥匙。
例如,如果 "x"
不在地图中,您希望发生什么?
- 创建一个新条目?那你就不能用
const
.
- 生成运行时错误?然后使用
find
或其他一些查找地图而不可能修改它的函数,而不是 []
.
operator[]
不是常量。如果键不存在,它会向该键添加一个值对象。
我认为您可以在 C++11 中基于范围的 for 循环中使用 const 引用,但是当我使用 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";
}
}
我收到这个错误:
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);
^
但是当我从基于范围的 for 循环中删除 const
时,它工作得很好。为什么我的代码无法使用 const 引用正常编译?
map
和 unordered_map
的 []
运算符需要在非常量对象上调用,因为如果不存在,它将更新对象以插入新条目对于那把钥匙。
例如,如果 "x"
不在地图中,您希望发生什么?
- 创建一个新条目?那你就不能用
const
. - 生成运行时错误?然后使用
find
或其他一些查找地图而不可能修改它的函数,而不是[]
.
operator[]
不是常量。如果键不存在,它会向该键添加一个值对象。