基于范围的循环中的const引用不起作用

时间:2017-04-26 01:30:03

标签: c++ c++11 reference ranged-loops

我认为你可以在C ++ 11中使用基于ranged-for-loops的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引用无法编译?

2 个答案:

答案 0 :(得分:4)

[]map的{​​{1}}运算符需要在非const对象上调用,因为它会更新对象以插入新条目(如果该条目不存在)键。

如果unordered_map不在地图中,您希望发生什么?

  • 创建新条目?然后,您无法使用"x"
  • 生成运行时错误?然后使用const或其他一些查找地图而不会对其进行修改的函数,而不是find

答案 1 :(得分:3)

operator[]不是常量。如果密钥不存在,则会向该密钥添加值对象。

改为使用unordered_map::at