//使用const char*,key为10000000时:unorder_mapcharcreate cost11.7611unorder_mapcharfind cost1.55619unorder_map std::stringcreate cost13.5376unorder_map std::stringfind cost2.33906//使用struct c_string,key为10000000时:unorder_mapcharcreate cost7.35524unorder_mapcharfind cost1.60826unorder_map std::stringcre...
//使用const char*,key为10000000时:unorder_mapcharcreate cost11.7611unorder_mapcharfind cost1.55619unorder_map std::stringcreate cost13.5376unorder_map std::stringfind cost2.33906//使用struct c_string,key为10000000时:unorder_mapcharcreate cost7.35524unorder_mapcharfind cost1.60826unorder_map std::stringcre...
#include <iostream> #include <string> #include <random> #include <unordered_map> #include <windows.h> using namespace std; using std::string; using std::random_device; using std::default_random_engine; string StrRand(int length) { char tmp; string buffer; random_device rd; default_rando...
key_type是键的类型。 value_type是值的类型。 构造函数 unordered_map可以以多种方式构造: // 默认构造std::unordered_map<int,std::string>myMap;// 构造并初始化std::unordered_map<int,std::string>myMap={{1,"one"},{2,"two"}};// 构造并指定初始容量std::unordered_map<int,std::string>myMa...
C++中map和unordered_map提供的是一种键值对容器,在实际开发中会经常用到,它跟Python的字典很类似,所有的数据都是成对出现的,每一对中的第一个值称之为关键字(key),每个关键字只能在map中出现一次;第二个称之为该关键字的对应值(value)。 map和unordered_map map是一种有序的容器,底层是用...
multimap<string, int> multiMap; cout << "multimap中的key值遍历(升序红黑树实现):" << endl; multiMap.insert({"B", 22}); multiMap.insert({"B", 11}); multiMap.insert({"A", 11}); multiMap.insert({"D", 44}); multiMap.insert({"C", 33}); for (auto& m : multiMap) cout << ...
当key可以强转成整形时(比如负数,指针等),用缺省的仿函数即可。当key是string这种不能强转成整形的类型时,就要手动写一个转换成整形的仿函数。上方是取string的第一个字符进行返回。同时也要手动传入这个仿函数。 这种取首字符的方法不是很好,下面是另一种字符串哈希算法: ...
unordered_map感觉速度和hash_map差不多,但是支持string做key,也可以使用复杂的对象作为key。 unordered_map编译时gxx需要添加编译选项:--std=c++11 unordered_map模板: template < class Key, // unordered_map::key_type class T, // unordered_map::mapped_type class Hash = hash<Key>, // unordered_ma...
使用map或者unordered_map进行字符串查找一般都是用std::string类型作为key,但是std::string的效率实在太低,不得不进行优化,尝试使用char*作key来查找。 一、map以char*为key 默认的map<char *,int>的key是指针,比较的是指针值的大小,不能进行字符串的匹配。
unordered_map和map类似,都是存储的key-value的值,可以通过key快速索引到value。不同的是unordered_map不会根据key的大小进行排序, 存储时是根据key的hash值判断元素是否相同,即unordered_map内部元素是无序的,而map中的元素是按照二叉搜索树存储,进行中序遍历会得到有序遍历。