<map>\<unordered_map>
来源:互联网 发布:海淘族信用数据脸谱 编辑:程序博客网 时间:2024/06/05 19:26
C++的map提供一种指定类型间的一对一映射关系,其基本的创建、查找、删除操作如下:
#include <map>#include <string>#include <iostream>using namespace std;map<int,string> int_string; //声明一个从整型到字符串型的映射,即int为key,string为valueint_string[404]='Not Found'; //这是最简单的建立映射的语法result=int_string.find(404); //map.find()返回一个迭代器指向键值,若该键不存在,则返回迭代器mao.end()if(result==int_string.end()){cout<<"we do not find 404"<<endl;}else{cout<<"we find 404";}int_string.erase(result); //删除404
由于map的元素默认按key的升序排列,所以它没有sort方法,下面是它的其它常用方法:
map.begin():返回指向头部的迭代器。
map.end():返回指向尾部的迭代器。
map.clear():清空map。
map.count():返回指定元素的出现次数。
map.size():返回map的大小。
map.max_size():返回map可容纳的最大元素数。
要想达到O(1)的查找时间复杂度,要用unordered_map,用空间换取时间。
unordered_map和哈希表很像,并且方法和map相似。
阅读全文
0 0
- unordered_map & map
- <map>\<unordered_map>
- map, hash_map,unordered_map介绍
- map, hash_map,unordered_map
- map与unordered_map
- c++ map unordered_map
- unordered_map map hash_map
- map, hash_map,unordered_map
- vector、map还是unordered_map?
- STL---哈希map:unordered_map
- hash_map,map,unordered_map效率
- map, hash_map,unordered_map
- map、hash_map和unordered_map
- map与unordered_map
- map与unordered_map
- map,hash_map, unordered_map
- map与unordered_map
- unordered_map和map
- 谈一下threadLocal的理解
- 如何卡SPFA
- Spring cloud系列一 包含所有基本要素的完整Spring Cloud demo
- JavaWeb 静态引入和动态引入
- rocketMQ简单总结
- <map>\<unordered_map>
- azure 最佳实践 3--最小协同操作
- Servlet知识点
- csapp 2e cachelab
- 机房合作感想
- SVN学习笔记
- 分析String在内存中的表现
- hadoop之hdfs
- Tomcat context.xml配置详解