原函数简化后如下:

void fun(const map<int,vector<int>> &mp, int index) {
for (auto tmp : mp[index]) {
//......
}
}

结果报错如下:

[Error] passing 'const std::map<int, std::vector<int> >' as 'this' argument of 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = int; _Tp = std::vector<int>; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, std::vector<int> > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::vector<int>; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]' discards qualifiers [-fpermissive]

经过长时间的查询大概问题就是出在,对于const的对象使用了非const的成员函数:std::map::[]本身不是const成员函数(操作符),对于不在map中的关键字,使用下标操作符会创建新的条目,改变了map。

解决办法可用如下:

  • 去掉const,这样有一定的安全风险
  • 拷贝map,有一定的性能开销
  • 对于C++11,可以使用map::at。它有const和non-const两个版本,对于找不到匹配关键字的情况,会抛出out_of_range。由于下标检查,也带来了性能代价。

结论:许多成员函数都设置了const和non-const两个版本,在这样的情况下就发挥了它的意义。今后使用时也应当注意功能相同或相似的函数之间细微的区别。

C++ | 使用const std::map,map::[]时遇到的一个bug的更多相关文章

  1. 在做vue计算属性,v-for处理数组时遇到的一个bug

    bug: You may have an infinite update loop in a component render function 无限循环 需要处理的数组(在 ** ssq **里): ...

  2. Tomcat运行javaweb项目时出现的一个bug

    Stacktrace:with root cause java.net.ConnectException: Connection refused:........................... ...

  3. VSCode调试data层时自身的一个bug

    使用VSCode在pytorch下进行调试有的时候会遇到下面这个错误: E00037.: Exception escaped from start_client Traceback (most rec ...

  4. java 创建匿名对象及声明map list时初始化

    java 创建匿名对象 类似于c# 中的 new { a:"aaa",b:"bbb"}; 1 创建匿名对象Object myobj = new Object() ...

  5. Error 2 error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)'

    Error 2 error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree ...

  6. could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'

    VS2008, 写一个简单的demo的时候出现了这个: 1>------ Build started: Project: GetExportTable, Configuration: Relea ...

  7. 笔记 freemark list标签迭代Map<Map<String,Object>集合排序问题

    本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处. 工作中出现一个比较特殊的问题,在模板ftl文件中,一般用list迭代map 举例: 后台: // 传入的参数 ...

  8. 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)'

    error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Tra ...

  9. Map.putAll方法——追加另一个Map对象到当前Map集合(转)

    该方法用来追加另一个Map对象到当前Map集合对象,它会把另一个Map集合对象中的所有内容添加到当前Map集合对象. 语法  putAll(Map<? extends K,? extends V ...

随机推荐

  1. 【2019.11.27】SDN上机第5次作业

    参考资料: https://www.cnblogs.com/zzqsss/p/11924685.html 问答环节 描述官方教程实现了一个什么样的交换机功能? Ryu是一个基于组件的软件定义的网络框架 ...

  2. jmap -heap 查看堆内存

    概述 用jmap -heap命令可以查看linux堆内存分布 具体用法 1:先查出tomcat的进程号 例如: 然后执行 jmap -heap 7095 可以打印出整体的堆信息   可以看到经过分配的 ...

  3. C# 最简单的使程序单进程运行的方法

    1.代码1 static void Main() { Process current = Process.GetCurrentProcess(); Process[] processes = Proc ...

  4. 【maven】插件和依赖管理

    1.插件管理 定义 pluginManagement 用来做插件管理的.它是表示插件声明,即你在项目中的pluginManagement下声明了插件,Maven不会加载该插件,pluginManage ...

  5. ISO/IEC 9899:2011 条款6——语言

    6 语言 6.1 标记法 1.在本条款中所使用的语法标记法.语义类别(非终结符)用斜体字指示,而字面量单词以及字符集成员(终结符)用粗体字指示.跟在一个非终结符后面的冒号(:)引出其定义.在单独的行中 ...

  6. PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)

    1080 Graduate Admission (30 分)   It is said that in 2011, there are about 100 graduate schools ready ...

  7. [LeetCode] 133. Clone Graph 克隆无向图

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  8. idea2018.1.1版激活码到2020.7

    N757JE0KCT-eyJsaWNlbnNlSWQiOiJONzU3SkUwS0NUIiwibGljZW5zZWVOYW1lIjoid3UgYW5qdW4iLCJhc3NpZ25lZU5hbWUiO ...

  9. 查看LINUX进程内存占用情况及启动时间

    可以直接使用top命令后,查看%MEM的内容.可以选择按进程查看或者按用户查看,如想查看oracle用户的进程内存使用情况的话可以使用如下的命令: (1) top top命令是Linux下常用的性能分 ...

  10. python类型检测最终指南--Typing模块的使用

    正文共:30429 字 预计阅读时间:76分钟 原文链接:https://realpython.com/python-type-checking/ 作者:Geir Arne Hjelle 译者:陈祥安 ...