//使用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时:un
测试系统liunx centos6.5 代码如下 插入的字符串是64位的字符串, 在并发1个情况下 在并发10的情况下 并发1000 并发5000 并发10000 一开始我以为char* 的速度会快,因为插入string的时候是要构造string申请内存的,可能是我的hash函数比系统的要慢了,但是
#include <string> #include <unordered_map> #include <cstring> #include <iostream> #include <tuple> using namespace std; tuple <int,char,char> kk; unordered_map<kk,int> map; int main() { map[1,"c","b"]=23; return 0; } 但这给了我以下错误:map.cpp:9:21: error: type/value ...
(char c : key) { h = (h << 5) + c; } return h; } }; struct MyEqual { bool operator()(const std::string& lhs, const std::string& rhs) const { // 自定义键比较函数实现 return lhs == rhs; } }; std::unordered_map<std::string, int, MyHash, My...
#include <string>#include <unordered_map> int main(){ // 创建三个 string 的 unordered_map (映射到 string ) std::unordered_map<std::string, std::string> u; u.reserve(10); std::cout << "load_factor:" << u.load_factor() << std::endl; std::cout << "max_load_factor:" <<...
std::unordered_map<std::string, double> m; /* 返回值类型为 pair<iterator, bool> 其中,迭代器执行新插入的元素,或已存在的元素 bool 表示是否插入成功 */ auto p = m.insert({ "sugar", 0.8 }); if (p.second) { std::cout << "插入成功\n"; } else { std::cout << "已存在,原值为...
#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...
使用map或者unordered_map进行字符串查找一般都是用std::string类型作为key,但是std::string的效率实在太低,不得不进行优化,尝试使用char*作key来查找。 一、map以char*为key 默认的map<char *,int>的key是指针,比较的是指针值的大小,不能进行字符串的匹配。
map<char, int>::iterator it = map1.begin(); map1.insert(it, pair<char, int>('x', 100)); 插入range 代码语言:javascript 代码运行次数:0 运行 AI代码解释 map<char, int> map2; map2.insert(map1.begin(), map1.find('c')); erase有三种用法: 通过key删除某个元素 代码语言:javascript ...
int> //typedef map<int,int> MapKey; //采用map //typedef hash_map<int,int> MapKey; //采用hash_map typedef unordered_map<int,int> MapKey; //采用unordered_map int GetPidMem(pid_t pid,string& memsize) { char filename[1024]; snprintf(filename,sizeof(filename),"/proc/%d/status",pid...