map 中的坑

map 中的坑

const map 无法使用map::operator[]

比如下面的代码在编译时会报错一大长串错误(ps: STL库报错就是这么长),仔细阅读,其实就是说const map

string getMapValue(const map<string,string> & mStr2Map,const string key)
{
    return mStr2Map[key];
}
error: passing ‘const std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >’ as ‘this’ argument of ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]’ discards qualifiers

请看map::operator[]的说明,即当x存在时,直接返回x对应的值;当x不存在时,会在map中添加key=x,即修改了map,也就是不符合const要求。

T& operator[] ( const key_type& x );

Access element

If x matches the key of an element in the container, the function returns a reference to its mapped value.

If x does not match the key of any element in the container, the function inserts a new element with that key and returns a reference to its mapped value. Notice that this always increases the map size by one, even if no mapped value is assigned to the element (the element is constructed using its default constructor).

A call to this function is equivalent to:
(*((this->insert(make_pair(x,T()))).first)).second

推荐用法

string getMapValue(const map<string,string> & mStr2Map,const string key)
{
    if(mStr2Map.find(key) != mStr2Map.end())
    {
        return mStr2Map.find(key)->second;
    }

    // 抛异常或直接返回空字符串
    return "";
}

map::find(key)->second 中 key不存在时不报错

map::find(key)->second 当key不存在时,返回map::end()->second, 并不报错,map::end()->second是随机值。

// map::find
#include <iostream>
#include <map>
using namespace std;

int main ()
{
    map<char,int> mymap;

    mymap['a']=50;
    mymap['b']=100;
    mymap['c']=150;

    // print content:
    cout << "elements in mymap:" << endl;
    cout << "a           => " << mymap.find('a')->second << endl;
    cout << "d           => " << mymap.find('d')->second << endl;
    cout << "mymap.end() => " << mymap.end()->second << endl;

    return 0;
}

输出结果:

elements in mymap:
a           => 50
d           => 0
mymap.end() => 0
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值