基于范围的循环中的const参考不起作用
const reference in range-based-for-loop not working
我以为您可以在C 11中的基于范围的循环中使用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);
^
但是,当我从基于范围的循环中删除const
时,它可以正常工作。为什么我的代码不使用const引用罚款?
operator[]
不是const。如果不存在密钥,它将在该密钥中添加一个值对象。
使用unordered_map::at
而不是。
map
和 unordered_map
的[]
操作员需要在非const对象上调用,因为如果该键不存在新条目,它将更新对象以插入新条目。p>例如,如果"x"
不在地图中,您想发生什么?
- 创建一个新条目?那么您不能使用
const
。 - 生成运行时错误?然后使用
find
或其他一些函数,这些功能在不潜在修改的情况下而不是[]
。
相关文章:
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- C++错误消息*成员参考.**初学者*
- 避免在基于反向范围的for循环实现中悬挂参考
- C++ 而循环问题 Codecademy 参考 [已解决]
- 在基于范围的循环中使用通用参考有什么好处?
- 基于范围的循环中的const参考不起作用
- 编译器无法在循环递归参考中找到功能
- 我怎样才能打破这个标准::shared_ptr参考循环
- 范围为循环常量参考似乎没有区别