原函数简化后如下:

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. idea的maven工程中修改pom会改变项目jdk版本

    解决办法 方案一 //pom中配置maven插件时候 <plugins> <!--jdk编译插件--> <plugin> <groupId>org.ap ...

  2. 可伸缩性架构常用技术——之数据切分 Data Sharding/Partition

    1. 简介 本来想写一篇可伸缩性架构方面的文章,发现东西太多了,久久未能下笔,这里首先把大家最关注的数据切分(Partition/Sharding)方面的内容先写完,给大家参考. 我们知道,为了应对不 ...

  3. ActionFilter、IAuthorizationFilter 权限验证重定向跳转到其它页面

    方法一: public class IsAllowAttribute: ActionFilterAttribute { public override void OnActionExecuting(A ...

  4. Netty集成Protobuf

    一.创建Personproto.proto 创建Personproto.proto文件 syntax = "proto2"; package com.example.protobu ...

  5. SQLServer string_split函数,撕裂函数,撕开函数

    declare @name char(1000) --注意:char(10)为10位,要是位数小了会让数据出错 set @name='s{sss}fc{fggh}dghdf{cccs}x' selec ...

  6. typescript - 2.数据类型

    typescript中为了使编写的代码更规范,更有利于维护,增加了类型校验,在typescript中主要给我们提供了以下数据类型 布尔类型(boolean) 数字类型(number) 字符串类型(st ...

  7. 关于Objective-C新增的__kindof关键字

    Objective-C随着Xcode 7的升级带来了许多新特性,当然此次更新最最大的就是引入了Objective-C的轻量级泛型,确切地说是Objective-C类的轻量级泛型.除此之外,还有一个小特 ...

  8. C语言 字符串切割

    #include <stdio.h> #include <stdlib.h> #include <string.h> /* 字符串切割函数 */ /* 知识补充: ...

  9. GCC版本中没有GLIBCXX_3.4.15错误

    解决错误呈现该错误的原因是当前的GCC版本中,没有GLIBCXX_3.4.15,须要安装更高版本.我们可以输入:strings /usr/lib64/libstdc++.so.6 | grep GLI ...

  10. [LeetCode] 349. Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...