大家都知道c++ stl中的map是用红黑树实现的,而tr1中实现了hash结构的map 为了不命名冲突被叫做了unordered_map,,
下面就给大家简单使用下这一结构,如果大家的结构在百万规模级就应该考虑使用该结构了。
#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_map>
using namespace std;
void print(std::pair<int,std::string> it)
{
cout << it.first << "-> " << it.second << endl;
}
void print2(std::pair<string,std::string> it)
{
cout << it.first << "-> " << it.second << endl;
}
int main(int argc, char* argv[])
{
typedef std::tr1::unordered_map<int,string> hash_map;
hash_map hm;
hm.insert(std::pair<int,std::string>(0,"Hello"));
hm[1] = "World";
for_each(hm.begin(),hm.end(),print);
std::tr1::unordered_map<string,string> hashcode_string;
hashcode_string.insert(std::pair<string,std::string>("opensss","release"));
hashcode_string["opensde"] = "ssss";
for_each(hashcode_string.begin(),hashcode_string.end(),print2);
system("pause");
return 0;
}
本文介绍了C++ STL中unordered_map的应用实例,通过具体的代码演示了如何使用基于哈希结构的unordered_map来存储和检索键值对,特别是在大规模数据集上的优势。
1万+

被折叠的 条评论
为什么被折叠?



