C++ 映射访问丢弃限定符 (const)

2024-05-12C/C++开发问题
3

本文介绍了C++ 映射访问丢弃限定符 (const)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

The following code says that passing the map as const into the operator[] method discards qualifiers:

#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 吗?

Is this because of the possible allocation that occurs on the map access? Can no functions with map accesses be declared 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,并且不能因为它的行为:

std::map's operator [] is not declared as const, and cannot be due to its behavior:

T&operator[] (const Key& key)

T& operator[] (const Key& key)

返回对映射到与键等效的键的值的引用,如果这样的键不存在,则执行插入.

Returns a reference to the value that is mapped to a key equivalent to key, performing insertion if such key does not already exist.

因此,您的函数不能声明为const,也不能使用地图的operator[].

As a result, your function cannot be declared const, and use the map's operator[].

std::mapfind() 函数允许您在不修改映射的情况下查找键.

std::map's find() function allows you to look up a key without modifying the map.

find() 返回一个iteratorconst_iteratorstd::pair 包含键(.first)和值(.second).

find() returns an iterator, or const_iterator to an std::pair containing both the key (.first) and the value (.second).

在 C++11 中,你也可以使用 at() 用于 std::map.如果元素不存在,该函数会抛出一个 std::out_of_range 异常,这与 operator [] 不同.

In C++11, you could also use at() for std::map. If element doesn't exist the function throws a std::out_of_range exception, in contrast to operator [].

这篇关于C++ 映射访问丢弃限定符 (const)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6