以下代码表示将地图作为const传递到operator[]方法中会丢弃限定符:

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

using namespace std;

class MapWrapper {
public:
    const int &get_value(const int &key) const {
        return _map[key];
    }

private:
    map<int, int> _map;
};

int main() {
    MapWrapper mw;
    cout << mw.get_value(42) << endl;
    return 0;
}

这是因为地图访问中可能出现的分配吗?没有地图访问的函数可以声明为const吗?

MapWrapper.cpp:10: error: passing ‘const std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >’ as ‘this’ argument of ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = int, _Tp = int, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, int> >]’ discards qualifiers

有帮助吗?

解决方案

std::mapoperator []未声明为const,并且不能归因于它的行为:

  

Ť安培<!>; operator [](const Key <!> amp; key)

     

返回对映射到等效于key的键的值的引用,如果此类键尚不存在则执行插入。

因此,您的函数无法声明operator[],并使用地图的find()

iteratorconst_iterator 功能允许您在不修改地图的情况下查找密钥。

std::pair 返回.first,或<= >到 .second ,其中包含密钥(at())和值(std::out_of_range)。

在C ++ 11中,您还可以使用 <=> 对于<=>。如果element不存在,则该函数抛出<=>异常,与<=>相反。

其他提示

您不能在operator[]的地图上使用const,因为该方法不是_map[key],因为它允许您修改地图(您可以指定为find)。请尝试使用<=>方法。

由于 operator[] 没有符合const限定的超载,因此不能安全地在const限定函数中使用。这可能是因为当前的重载是为了返回和设置键值而建立的。

相反,您可以使用:

VALUE = map.find(KEY)->second;

或者,在C ++ 11中,您可以使用 at() 运算符:

VALUE = map.at(KEY);

一些较新版本的GCC头文件(我机器上的4.1和4.2)具有非标准成员函数map :: at(),如果键不在地图中,则声明为const和throw std :: out_of_range。 / p>

const mapped_type& at(const key_type& __k) const

根据函数注释中的引用,似乎已将其建议为标准库中的新成员函数。

首先,您不应该使用以_开头的符号,因为它们是为语言实现/编译器编写者保留的。对于某人的编译器来说,_map很容易成为语法错误,除了你自己,你没有人可以责备。

如果你想使用下划线,请把它放在最后,而不是开头。您可能犯了这个错误,因为您看到了一些Microsoft代码。记住,他们编写自己的编译器,因此他们可以逃脱它。即便如此,这也是一个坏主意。

operator []不仅返回引用,它实际上在地图中创建了条目。所以你不只是得到一个映射,如果没有,你就是在创建一个映射。那不是你想要的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top