C++ std::map::erase用法及其陷阱】的更多相关文章

1.引入: STL的map中有一个erase方法用来从一个map中删除制定的节点 eg: map<string,string> mapTest; typedef map<string,string>::iterator ITER; ITER iter=mapTest.find(key); mapTest.erase(iter); 像上面这种删除单个节点,map的行为不会出现问题,但是当在一个循环里用的时候,往往会被误用. 2.陷阱 eg: for(ITER iter=mapTest…
STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用.    在STL模板类中,用于线性数据存储管理的类主要有vector, list, map 等等.本文主要针对map对象,结合自己学习该对象的过程,讲解一下具体用法.本人初学,水平有限,讲解差错之处,请大家多多批评指正.     map对象所实现的功能跟MFC得CMap相似,但是根据一些文章的介绍和论述,MFC CMap在个方面都与STL map有一定的差距,例如不是C++标准,不支持赋值构造,对象化概…
map::erase函数在不同版本stl中的差异 1. C++98和C++11标准 http://www.cplusplus.com/reference/map/map/erase/ 2. pj stl(windows) map::erase函数的windows实现版本(C++11标准)会返回一个map::iterator: iterator map::erase(const_iterator _Where); iterator map::erase(const_iterator _First,…
总结一下map::erase的正确用法. 首先看一下在循环中使用vector::erase时我习惯的用法: for(vector<int>::iterator it = vecInt.begin(); it != vecInt.end();) { if(*it == 0) { it = vecInt.erase(it); } else { it++; } } 程序从一个vector中删除值为0的元素,利用了vector::erase函数根据iterator删除某个元素时会返回下一个元素的ite…
#include "map" //引入头文件 初始化: std::map <int, std::string> _map1; //初始化 //c++11中引入的,可以直接在初始化时赋值 std::map <int, std::string> _map = { {0,"11"}, {2,"22"}, {3,"33"}, }; 插入: // 如果已经存在键值200,则会作赋值修改操作,如果没有则插入 _ma…
c++map的用法 分类: 资料 2012-11-14 21:26 10573人阅读 评论(0) 收藏 举报 最全的c++map的用法 此文是复制来的0.0 1. map最基本的构造函数: map<string ,int>mapstring; map<int,string >mapint; map<sring,char>mapstring; map< char ,string>mapchar; map<char,int>mapchar; map&…
map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道.这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处. 下面举例说明什么是一对一的数据映射.比如一个班级中,每个学生的学号跟他的姓名就存在着一一…
最全的c++map的用法 1. map最基本的构造函数:map<string ,int>mapstring; map<int,string >mapint;map<sring,char>mapstring; map< char ,string>mapchar;map<char,int>mapchar; map<int ,char>mapint: 2. map添加数据: map<int ,string>maplive;1. …
C++ 中经常使用的容器类有vector,list,map.其中vector和list的erase都是返回迭代器,但是map就比较不一样. 当在循环体中使用map::erase语句时,为了能够在任何机器上编译通过,并且能够跨平台(windows.linux),正确的写法是: map<int, int> l_map; map<int, int>::iterator l_iter = l_map.begin(); map<int, int>::iterator l_iter…
转载自:http://blog.csdn.net/sunquana/article/details/12576729 一.定义   (1) map<string,   int>   Map;     (2) 或者是:typedef   map<string,int>   Mymap;                       Mymap   Map; 二.插入数据  插入数据之前先说一下pair 和 make_pair 的用法pair是一个结构体,有first和second 两个…
二叉搜索树是ACM中经常需要用到的数据结构,熟练掌握map和set的用法很关键,现对其做一个简单的总结. 主要的功能有:插入元素,查找元素,删除,遍历/反向遍历. 插入,删除和查找操作的时间都和树的高度成正比.即如果有n个元素,每次操作的时间复杂度是O(logn). 现以map为例说明用法,multimap是可以插入重复键值的元素的map. #include<iostream> #include<cstdio> #include<cstdlib> #include<…
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道.这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处. 下面举例说明什么是一对一的数据映射.比如一个班级中,每个学生的学号跟他的姓名就存在着一一…
<算法笔记>学习笔记 map 常见用法详解 map翻译为映射,也是常用的STL容器 map可以将任何基本类型(包括STL容器)映射到任何基本类型(包括STL容器) 1. map 的定义 //单独定义一个map map<typename1, typename2> mp; //map和其他的STL容器在定义上有点不同 //因为 map 需要确定映射前类型(键key)和映射后类型(值value) //其中第一个是键的类型,第二个是值的类型 //字符串到整型的映射 //必须使用string…
std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less<Key>, // map::key_compare class Alloc = allocator<pair<const Key,T> > // map::allocator_type > class map; Map Maps are associative co…
map<T, T>是C++的STL中存储key-value键值对数据结构的最基础的模板类,相对于multimap可以重复的key值,map的key是非重复的. C++的reference这样说明的: std::map is a sorted associative container that contains key-value pairs with unique keys. Keys are sorted by using the comparison function Compare.…
1.例: map<int,string> m_mapTest; m_mapTest.insert(make_pair(1,"kong")); m_mapTest.insert(make_pair(2,"yang")); m_mapTest.insert(make_pair(1,"hello1")); m_mapTest.insert(make_pair(3,"hello3")); m_mapTest.insert(…
昨天晚上,我徒弟跑过来讲,他的程序的内存占用居高不下,愿意是std::map的clear()没有效果.于是我让他用erase(begin,end); 试试也不行. 代码如下: void release_map(void) { map<int,string> testmap; ; i<; i++) { testmap.insert(make_pair(i,"abc")); } testmap.clear(); } int main() { release_map();…
using namespace std; std::map<int,int> m_map; 1.添加 for(int i=0;i<10;i++) { m_map.insert(make_pair(i,i)); } 2.修改 std::map<int,int>::iterator iter; for(iter=m_map.begin();iter != m_map.end();iter++) { int& i=iter->second;//这个地方用别名,就可以修…
由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊的函数,lower_bound.upper_bound.equal_range. 原型如下: iterator lower_bound (const value_type& val) const; iterator upper_bound (const value_type& val) con…
#include <queue>#include <map>#include <iostream>#include <string.h> class TestU {public: TestU(char *); ~TestU(); char *getData();private: char *data;}; TestU::TestU(char *d){ if(d) { int len = strlen(d); data = new char[len + 1];…
void eraseMap() { int n = sizeof(MmMethod); std::map<CString, int>mapDemo; ; i < ; i++) { CString strKey; strKey.Format(_T("key:%d"), i); mapDemo.insert(std::make_pair(strKey, i)); } for(auto it = mapDemo.begin(); it != mapDemo.end(); /…
首先我们讲遍历std::map, 大部分人都能写出第一种遍历的方法,但这种遍历删除的方式并不太安全. 第一种 for循环变量: #include<map> #include<string> #include<iostream> using namespace std; int main() { map<int,string*> m; m[1]= new string("1111111111111111"); m[2]= new strin…
vector::erase()返回下一个iter: STL中的源码: //清除[first, last)中的所有元素 iterator erase(iterator first, iterator last) { iterator i = copy(last, finish, first); //[last, finish)拷贝到first destroy(i, finish); finish = finish - (last - first); return first; } //清除某个位置…
1. map::at #include <iostream> #include <string> #include <map> using namespace std; int main(){ map<string, int> mymap = { {}, {}, {}}; mymap.at(; mymap.at(; mymap.at(; for (auto& x:mymap){ cout<<x.first<<": &…
entrySet用法 以及遍历map的用法   keySet是键的集合,Set里面的类型即key的类型entrySet是 键-值 对的集合,Set里面的类型是Map.Entry   1.keySet() Map map=new HashMap(); Iterator it=map.keySet().iterator(); Object key; Object value; while(it.hasNext()){ key=it.next(); value=map.get(key); System…
From: https://www.walletfox.com/course/mapwithcustomclasskey.php If you have ever tried to use a custom class as a key of std::map, most probably you got a compilation error. This article explains why this happens and shows how to make custom classes…
在上述的代码中,红色波浪线的部分编译的时候报错: error C2976: 'std::map' : too few template arguments 换成std::map<std::string, std::string>也是一样的错误. 怎么回事那? [解决方法] 包含 map就可以了. #include <map>…
#include<iostream> #include<map> #include<string> #define s second #define f first using namespace std; // map 的构造 map<int , int> ii; map<string , string> ss; map<string , int> si; typedef map<int , string> mapTyp…
使用std::map和std::list存放数据,消耗内存比实际数据大得多 场景:项目中需要存储一个结构,如下程序段中TEST_DATA_STRU,结构占24B.但是使用代码中的std::list<DataListMap>类存储4000个DataListMap,每个DataListMap中有4个pairs,每个pair中的DataList中有6000个items时,消耗掉的内存几乎是我们存放TEST_DATA_STRU的2倍. #include <iostream> #includ…
python map 常见用法2017年02月01日 19:32:41 淇怪君 阅读数:548版权声明:欢迎转载,转载请注明出处 https://blog.csdn.net/Tifficial/article/details/54810083 list 合并12345678 >>> list1 = [11,22,33]>>> map(None,list1)[11, 22, 33]>>> list1 = [11,22,33]>>> li…