相关疑难解决方法(0)

C++ const std :: map引用无法编译

是否有理由将参数传递给std::mapas作为导致[]运算符中断?当我使用const时,我得到这个编译器错误(gcc 4.2):

错误:'map [name]'中'operator []'不匹配

这是函数原型:

void func(const char ch, std::string &str, const std::map<std::string, std::string> &map);
Run Code Online (Sandbox Code Playgroud)

而且,我应该提一下,当我删除const前面的关键字时没有问题std::map.

如果我被正确指示,[]运算符实际上会在地图中找到一个新对,如果找不到密钥,这当然可以解释为什么会发生这种情况,但我无法想象这会是可接受的行为

如果有更好的方法,比如使用find代替[],我会很感激.我似乎无法找到工作,虽然...我收到const不匹配的迭代器错误.

c++ stdmap std find operator-keyword

15
推荐指数
2
解决办法
1万
查看次数

标签 统计

c++ ×1

find ×1

operator-keyword ×1

std ×1

stdmap ×1