#include <map>

map<string,int> dict;

map是基于红黑树实现的,可以快速查找一个元素是否存在,是关系型容器,能够表达两个数据之间的映射关系。

dict.insert(make_pair("abc",1));

dict.count("mn"); 看看dict中含有 mn的个数,因为元素是唯一的,所以这个返回值只有两种情况,0或者1. (multi_map就不是这样啦)

dict.find("mn");如果不存在就返回 dict.end(),如果存在就返回指向该元素的 iterator

dict["pq"]++ 相当于取出 value 来进行 ++ 再存回去,下标访问不存在的元素,将导致在map中添加一个新的元素,新的键就是该下标, value的值为默认值。

所以建立一个map的时候可以:

vector<string> L;
unordered_map<string,int> dictL;
for(int i = ; i< L.size(); i++)
dictL[L[i]] += ;

删除元素: dict.eraser("abc"); 返回值为删除元素的个数

dict.eraser(itr); 删除 itr 指向的元素,并且 itr 所指向元素必须是存在的,不能是 dict.end(). 这种形式的返回值为 void

遍历: for(map<string, int>::iterator itr = dict.begin(); itr != dict.end(); itr++)

cout<< itr->first <<"  " << itr->second <<endl;

map中的元素是按照健,有序的.

#include<unordered_map>

unordered_map是基于hash实现的。

unordered_map的插入、删除、查找 性能都优于 hash_map 优于 map,所以首选为 unordered_map.

它的缺陷是元素是无序的,当使用时候需要元素是有序的时候,不可以用它。

性能比较参考:http://keary.cn/?p=779

下面是它比较的代码

#include <iostream>
#include <stdlib.h>
#include <Windows.h>
#include <map>
#include <hash_map>
#include <unordered_map>
#include <algorithm> bool MapTest()
{
const unsigned int NUM_COUNT = 0xffffff; // 数组长度
const int DEFAULT_VALUE = ; // 键值
const unsigned int NUM_RANGE = 0xffffff; // 随机数范围的最大值 int* szNumA = new int[NUM_COUNT];
int* szNumB = new int[NUM_COUNT]; srand(::GetTickCount()); for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
{
szNumA[uiNum] = (rand() * rand()) % NUM_RANGE;
szNumB[uiNum] = szNumA[uiNum];
}
for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
{
std::swap(szNumB[uiNum], szNumB[(rand() * rand()) % NUM_COUNT]);
} std::map<int, int> mapNum;
std::hash_map<int, int> hMapNum;
std::unordered_map<int, int> unMapNum; DWORD dwMap, dwHMap, dwUnMap; // 插入测试
dwMap = ::GetTickCount();
for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
{
mapNum.insert(std::map<int, int>::value_type(szNumA[uiNum], ));
}
dwMap = ::GetTickCount() - dwMap; dwHMap = ::GetTickCount();
for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
{
hMapNum.insert(std::hash_map<int, int>::value_type(szNumA[uiNum], ));
}
dwHMap = ::GetTickCount() - dwHMap; dwUnMap = ::GetTickCount();
for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
{
unMapNum.insert(std::unordered_map<int, int>::value_type(szNumA[uiNum], ));
}
dwUnMap = ::GetTickCount() - dwUnMap; std::cout << "insert time of map is :" << dwMap << "ms" <<std::endl;
std::cout << "insert time of hash_map is :" << dwHMap << "ms" <<std::endl;
std::cout << "insert time of unordered_map is :" << dwUnMap << "ms" <<std::endl << std::endl; // 查找测试
dwMap = ::GetTickCount();
for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
{
mapNum.find(szNumB[uiNum]);
}
dwMap = ::GetTickCount() - dwMap; dwHMap = ::GetTickCount();
for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
{
hMapNum.find(szNumB[uiNum]);
}
dwHMap = ::GetTickCount() - dwHMap; dwUnMap = ::GetTickCount();
for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
{
unMapNum.find(szNumB[uiNum]);
}
dwUnMap = ::GetTickCount() - dwUnMap; std::cout << "search time of map is :" << dwMap << "ms" <<std::endl;
std::cout << "search time of hash_map is :" << dwHMap << "ms" <<std::endl;
std::cout << "search time of unordered_map is :" << dwUnMap << "ms" <<std::endl << std::endl; // 删除测试
dwMap = ::GetTickCount();
for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
{
mapNum.erase(szNumB[uiNum]);
}
dwMap = ::GetTickCount() - dwMap; dwHMap = ::GetTickCount();
for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
{
hMapNum.erase(szNumB[uiNum]);
}
dwHMap = ::GetTickCount() - dwHMap; dwUnMap = ::GetTickCount();
for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
{
unMapNum.erase(szNumB[uiNum]);
}
dwUnMap = ::GetTickCount() - dwUnMap; std::cout << "delete time of map is :" << dwMap << "ms" <<std::endl;
std::cout << "delete time of hash_map is :" << dwHMap << "ms" <<std::endl;
std::cout << "delete time of unordered_map is :" << dwUnMap << "ms" <<std::endl << std::endl; delete [] szNumA;
delete [] szNumB; return true;
} int main(int argc, char* argv[])
{
MapTest(); system("pause");
return ;
}

最新文章

  1. WebSocket与消息推送
  2. JS IOS/iPhone的Safari不兼容Javascript中的Date()问题
  3. Jenkins的安装与配置
  4. UML 简单介绍
  5. Mongodb For C# &quot;Query&quot; 对象常用的方法
  6. Android网络通信库Volley简介
  7. Jquery中Ajax异步请求中的async参数的作用
  8. 调试php的soapServer
  9. C#DbHelperOleDb,Access数据库帮助类 (转载)
  10. android:showAsAction="never"是做什么用的?
  11. 格式化输出prettify()
  12. 17 ContentProvider
  13. luogu4159 迷路 (矩阵加速)
  14. UVA 2519 Radar Installtion
  15. Rplidar学习(五)—— rplidar使用cartographer_ros进行地图云生成
  16. [转]Java之JMX 详解
  17. Oracle10g 64位 在Windows 2008 Server R2 中的安装 解决方案
  18. java环境配置及原理详解
  19. php分享二十三:字符编码
  20. InstallShield卸载状态

热门文章

  1. Java集合框架汇总
  2. Java总结 - List实现类ArrayList&amp;LinkedList
  3. 20181229(守护进程,互斥锁,IPC,生产者和消费者模型)
  4. 基于Ajax提交formdata数据、错误信息展示和局部钩子、全局钩子的校验。
  5. BFS:Open and Lock(一个数的逐位变化问题的搜索)
  6. [Hdu3507]Print Article(斜率优化)
  7. Git-Git初始化
  8. 求 1 到 n 的所有数的约数和
  9. cf975d Ghosts
  10. loj2062 [HAOI2016]地图