有关std::map和std::vector的使用】的更多相关文章

使用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…
先说map吧. 最需要注意的就是:用下标访问map中的元素时,与使用下标访问vector的行为截然不同! 用下标访问不存在的元素时,将导致在map容器中添加一个新的元素,它的键即为该下标! 然而很多时候,程序员的本意并非如此. 不过,这也是向map中添加元素除了insert之外的另一种方法. 对于vector我想说的是:采用下标的方式给vector赋值也是可以的, 只要你在赋值之前调用resize. 好了,今天就总结这么多了. 哦,对了,对于map.可以用成员函数find来防止出现上面的情况…
原函数简化后如下: 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,…
#include "map" //引入头文件 初始化: std::map <int, std::string> _map1; //初始化 //c++11中引入的,可以直接在初始化时赋值 std::map <int, std::string> _map = { {0,"11"}, {2,"22"}, {3,"33"}, }; 插入: // 如果已经存在键值200,则会作赋值修改操作,如果没有则插入 _ma…
STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用.    在STL模板类中,用于线性数据存储管理的类主要有vector, list, map 等等.本文主要针对map对象,结合自己学习该对象的过程,讲解一下具体用法.本人初学,水平有限,讲解差错之处,请大家多多批评指正.     map对象所实现的功能跟MFC得CMap相似,但是根据一些文章的介绍和论述,MFC CMap在个方面都与STL map有一定的差距,例如不是C++标准,不支持赋值构造,对象化概…
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…
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(…
1.对Key排序. std::map的第三个参数即为对key进行排序的比较函数.默认为less,表示升序.如果要降序,可以改为greater. 2.对Value排序 不支持,因为map不是一个序列的容器.如果真要排序,需要转为一个保存pair的vector,再排序. 不过这样性能就受损了,建议更换容器. 详细参考: https://blog.csdn.net/puqutogether/article/details/41889579 http://www.cplusplus.com/refere…
使用STL中的map时候,有时候需要使用结构题自定义键值,比如想统计点的坐标出现的次数 struct Node{ int x,y; }; ...... map<Node,int>mp; mp[(Node){x,y}]++; 这样子的话,会出现一堆报错 c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_function.h||In instantiation of 'bool std::less<_Tp>::operator()(…
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…