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 ...
随机推荐
- GIL全局解释器锁,线程池与进程池 同步异步,阻塞与非阻塞,异步回调
GIL全局解释器锁 1.什么是GIL 官方解释:'''In CPython, the global interpreter lock, or GIL, is a mutex that prevents ...
- pandas中截取一列字符串中每行字符串的一部分
import pandas as pd df = pd.DataFrame([[',1], [',2], [',3], [',4], [',5], [',6]],columns=['str','num ...
- beautifulsoap爬虫
从html文件读 from bs4 import BeautifulSoup html_doc="文件地址" html_file=open(html_doc,"r&quo ...
- iis实现方向代理
将请求的网址重写重定向到其它网址.当80端口被占用无法同时使用两个Web服务的解决方案,使得IIS和Apache Tomcat 共存 0|1环境 WindowServer 2008 IIS7 Apac ...
- position: sticky 防坑指南
position: sticky 防坑指南:https://www.jianshu.com/p/e217905e8b87 今天在写小程序项目的时候碰到一个需求是要把轮播图下面的标签栏滑动到顶部后固定, ...
- [开源] LaravelPlus - 基于 Laravel 魔改,为方便实际业务使用 - 开发中
目的 为了减少重复 CURD 和新项目的配置麻烦等问题,(就是为了骗星星:LaravelPlus )如: 现有的 infyomlabs/laravel-generator CODE 生成工具虽然好用, ...
- Mysql-Sqlalchemy-ORM-many_to_many
orm_m2m.py from sqlalchemy import Table,Column,Integer,String,DATE, ForeignKey from sqlalchemy.orm i ...
- 美团2017年CodeM大赛-初赛B轮 黑白树 (树形dp)
大意: 给定树, 初始每个点全为白色, 点$i$有权值$k_i$, 表示选择$i$后, 所有距离$i$小于$k_i$的祖先(包括i)会变为黑色, 求最少选多少个点能使所有点变为黑色. 链上情况的话, ...
- windowsformshost mouse event not transmit to it's parent control
in the case you can do it to fix: MouseEventArgs e = new MouseEventArgs(Mouse.PrimaryDevice, 0); e.R ...
- dev chart使用
public class LineChartHelp { #region 折线图 /// <summary> /// 创建折线图 /// </summary> public v ...