std::map 的swap错用
map<int, shared_ptr<int>>map_test;
shared_ptr<int> tmp_1 = make_shared<int>();
map_test[] = tmp_1; shared_ptr<int> tmp_20 = make_shared<int>();
shared_ptr<int> tmp_vice1 = map_test[]; tmp_vice1.swap(tmp_20);
今天错误的使用了swap 如以上代码,本意是想改变map_test[1]所对应的值为20
可是这样使用后 是把tmp_vice1指向了原先tmp_20所管理的内存,tmp_20指向了 tmp_1管理的内存 而不是内存的相互交换!
正确使用直接用map_test[1] 和tmp_20 swap即可
std::map 的swap错用的更多相关文章
- C++ std::map
std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...
- Std::map too few template arguments
在上述的代码中,红色波浪线的部分编译的时候报错: error C2976: 'std::map' : too few template arguments 换成std::map<std::str ...
- std::map自定义类型key
故事背景:最近的需求需要把一个结构体struct作为map的key,时间time作为value,定义:std::map<struct, time> _mapTest; 技术调研:众所周知, ...
- C++ | 使用const std::map,map::[]时遇到的一个bug
原函数简化后如下: void fun(const map<int,vector<int>> &mp, int index) { for (auto tmp : mp[i ...
- std::map使用结构体自定义键值
使用STL中的map时候,有时候需要使用结构题自定义键值,比如想统计点的坐标出现的次数 struct Node{ int x,y; }; ...... map<Node,int>mp; m ...
- C++ std::map用法简介
#include "map" //引入头文件 初始化: std::map <int, std::string> _map1; //初始化 //c++11中引入的,可以直 ...
- std::map用法
STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用. 在STL模板类中,用于线性数据存储管理的类主要有vector, list, map 等等.本文主要 ...
- C++ std::map::erase用法及其陷阱
1.引入: STL的map中有一个erase方法用来从一个map中删除制定的节点 eg: map<string,string> mapTest; typedef map<string ...
- std::map
1.例: map<int,string> m_mapTest; m_mapTest.insert(make_pair(1,"kong")); m_mapTest.ins ...
随机推荐
- GAN(生成对抗网络)之keras实践
GAN由论文<Ian Goodfellow et al., “Generative Adversarial Networks,” arXiv (2014)>提出. GAN与VAEs的区别 ...
- windows下的句柄利用
什么是句柄 维基百科:在程序设计中,句柄(handle)是Windows操作系统用来标识被应用程序所建立或使用的对象的整数.其本质相当于带有引用计数的智能指针.当一个应用程序要引用其他系统(如数据库. ...
- 用Blackbox Exporter to Monitor web和端口
1.按照exporter .wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.12.0/blackbo ...
- Transformer详解
0 简述 Transformer改进了RNN最被人诟病的训练慢的缺点,利用self-attention机制实现快速并行. 并且Transformer可以增加到非常深的深度,充分发掘DNN模型的特性,提 ...
- JS动态添加的标签无法绑定事件解决方案~~~
今天用ajax实现动态插入数据时发现监听一直不起作用,一样的代码,非动态的就可以监听实现 这是困扰了我近一个小时的bug,后面才理解到可能是动态插入导致的! 看了看网上的解决方案,似乎都不太通俗,讲的 ...
- Appendix 1- LLN and Central Limit Theorem
1. 大数定律(LLN) 设Y1,Y2,……Yn是独立同分布(iid,independently identically distribution)的随机变量,A = SY /n = (Y1+...+ ...
- shell中防止意外发生的两个实用的设置
set -o nounset set -o errexit set -o nounset 在默认情况下,遇到不存在的变量,会忽略并继续执行,而这往往不符合预期,加入该选项,可以避免恶果扩大,终止脚本的 ...
- MySQL5.7主从-GTID-mysqldump,xtrabackup搭建
1.两个空库,都是row+gtid,版本为MySQL5.7.22mydb1执行:(dba_user@localhost) [(none)]> show master status;+------ ...
- python爬虫实战--抖音
申明&警告: 请在相关网站的许可范围内爬取数据.以免影响网站正常运行, 如果我的文章有触犯权益的地方, 请告知删除. 上一篇爬取知乎的文章基本就是大多数网站的爬取思路了(headers部分其实 ...
- T100——动态更改Label的说明
例子: #設定科目名稱 IF g_prog = 'aapt300' THEN CALL cl_set_comp_att_text("lbl_apca036",cl ...