operator c+=_如何在C ++中使用operator []访问const映射中的元素?

operator c+=

How to operator[] access element in a const map in C++? For example, the compiler will report error on this piece of code:

如何在C ++中使用operator[]访问const映射中的元素? 例如,编译器将报告以下代码错误

#include <iostream>
#include <string>
#include <map>

std::int64_t count(const std::map<std::string, std::int64_t>& map)
{
  return map["one"] + map["two"];
}

int main ()
{

  std::map<std::string, std::int64_t> map = {
    {"one", 1},
    {"two", 2}
  };

  std::cout << count(map) << std::endl;

  return 0;
}

g++, using command

g++ ,使用命令

$ g++ --std=c++11 main.cc -o a

will report

将报告

main.cc: In function ‘int64_t count(const std::map<std::basic_string<char>, long int>&)’:
main.cc:7:19: error: passing ‘const std::map<std::basic_string<char>, long int>’ as ‘this’ argument of ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = std::basic_string<char>; _Tp = long int; _Compare = std::less<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, long int> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = long int; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::basic_string<char>]’ discards qualifiers [-fpermissive]
   return map["one"] + map["two"];
                   ^
main.cc:7:32: error: passing ‘const std::map<std::basic_string<char>, long int>’ as ‘this’ argument of ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = std::basic_string<char>; _Tp = long int; _Compare = std::less<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, long int> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = long int; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::basic_string<char>]’ discards qualifiers [-fpermissive]
   return map["one"] + map["two"];
                                ^

To access the data of a const map in C++, use the at() member function,

要在C ++中访问const映射的数据,请使用at()成员函数

const mapped_type & at (const key_type &__k) const

instead of operator[]. The reason why operator[] can not be used for a const map is because that, if the key from operator[] does not exist, a pair with that key is created using default values and then returned, which will actually change the map.

代替operator[] 。 无法将operator[]用于const映射的原因是,如果不存在来自operator[]键,则会使用默认值创建与该键的一对,然后将其返回,这实际上将更改映射。

Note that at() may throw std::out_of_range exception if no such data is present.

请注意,如果没有此类数据,则at()可能会引发std::out_of_range异常。

For the code in the question, to make it work, change

对于问题中的代码,使其生效,请进行更改

std::int64_t count(const std::map<std::string, std::int64_t>& map)
{
  return map["one"] + map["two"];
}

to

std::int64_t count(const std::map<std::string, std::int64_t>& map)
{
  return map.at("one") + map.at("two");
}
Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。

翻译自: https://www.systutorials.com/how-to-operator-access-element-in-a-const-map-in-c/

operator c+=

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值