STL中的hash_map

简介: 看下MS的使用例子:// hash_map_hash_map.cpp// compile with: /EHsc#include #include #include #include #include us...

看下MS的使用例子:

// hash_map_hash_map.cpp // compile with: /EHsc #include <ostream> #include <hash_map> #include <iostream> #include <fstream> #include <string.h> using namespace std; typedef char * MyStr; struct MyInt { int i; friend ostream& operator<<(ostream& ii, MyInt& jj); MyInt(int j = 0) { i = j; } }; struct greater_str { bool operator()(const MyStr & x, const MyStr & y) const { if (strcmp(x, y) < 0) return true; return false; } }; struct less_str { bool operator()(const MyStr & x, const MyStr & y) const { if (strcmp(x, y) > 0) return true; return false; } }; ostream& operator<<(ostream& o, MyInt& j) { o << j.i; return o; } int main() { using namespace stdext; typedef pair <MyStr, MyInt> Int_Pair; hash_map<MyStr, MyInt>::iterator hm1_Iter, hm3_Iter, hm4_Iter, hm5_Iter, hm6_Iter; hash_map<MyStr, MyInt, hash_compare<MyStr, greater_str> >::iterator hm2_Iter; // Create an empty hash_map hm0 of key type integer hash_map<MyStr, MyInt> hm0; // Create an empty hash_map hm1 with the key comparison // function of less than, then insert 4 elements hash_map<MyStr, MyInt, hash_compare<MyStr, less_str> > hm1; hm1.insert(Int_Pair("one", 0)); hm1.insert(Int_Pair("two", 10)); hm1.insert(Int_Pair("three", 20)); hm1.insert(Int_Pair("four", 30)); hm1.insert(Int_Pair("five", 40)); // Create an empty hash_map hm2 with the key comparison // function of geater than, then insert 2 elements hash_map<MyStr, MyInt, hash_compare<MyStr, greater_str> > hm2; hm2.insert(Int_Pair("one", 10)); hm2.insert(Int_Pair("two", 20)); // Create a hash_map hm3 with the // allocator of hash_map hm1 hash_map<MyStr, MyInt>::allocator_type hm1_Alloc; hm1_Alloc = hm1.get_allocator(); hash_map<MyStr, MyInt, hash_compare<MyStr, less_str> > hm3(hash_compare<MyStr, less_str > (), hm1_Alloc); hm3.insert(Int_Pair("three", 30)); // Create a copy, hash_map hm4, of hash_map hm1 hash_map<MyStr, MyInt, hash_compare<MyStr, less_str> > hm4(hm1); // Create a hash_map hm5 by copying the range hm1[_First, _Last) hash_map<MyStr, MyInt>::const_iterator hm1_bcIter, hm1_ecIter; hm1_bcIter = hm1.begin(); hm1_ecIter = hm1.begin(); hm1_ecIter++; hm1_ecIter++; hash_map<MyStr, MyInt> hm5(hm1_bcIter, hm1_ecIter); // Create a hash_map hm6 by copying the range hm4[_First, _Last) // and with the allocator of hash_map hm2 hash_map<MyStr, MyInt>::allocator_type hm2_Alloc; hm2_Alloc = hm2.get_allocator(); hash_map<MyStr, MyInt, hash_compare<MyStr, less_str> > hm6(hm4.begin(), ++hm4.begin(), hash_compare<MyStr, less_str > (), hm2_Alloc); cout << "hm1 ="; for (hm1_Iter = hm1.begin(); hm1_Iter != hm1.end(); hm1_Iter++) cout << " " << hm1_Iter -> second; cout << endl; cout << "hm2 ="; for (hm2_Iter = hm2.begin(); hm2_Iter != hm2.end(); hm2_Iter++) cout << " " << hm2_Iter -> second; cout << endl; cout << "hm3 ="; for (hm3_Iter = hm3.begin(); hm3_Iter != hm3.end(); hm3_Iter++) cout << " " << hm3_Iter -> second; cout << endl; cout << "hm4 ="; for (hm4_Iter = hm4.begin(); hm4_Iter != hm4.end(); hm4_Iter++) cout << " " << hm4_Iter -> second; cout << endl; cout << "hm5 ="; for (hm5_Iter = hm5.begin(); hm5_Iter != hm5.end(); hm5_Iter++) cout << " " << hm5_Iter -> second; cout << endl; cout << "hm6 ="; for (hm6_Iter = hm6.begin(); hm6_Iter != hm6.end(); hm6_Iter++) cout << " " << hm6_Iter -> second; cout << endl; }  

输出:

hm1 = 20 0 30 40 10 hm2 = 10 20 hm3 = 30 hm4 = 20 0 30 40 10 hm5 = 0 20 hm6 = 20  

 

 

 

补充 operator [] 和 insert 的区别,可以看出当不存在时,则调用insert。

mapped_type& operator[](const key_type& _Keyval) { // find element matching _Keyval or insert with default mapped iterator _Where = this->lower_bound(_Keyval); if (_Where == this->end()) _Where = this->insert(value_type(_Keyval, mapped_type())).first; return ((*_Where).second); } }; 

相关文章
|
6月前
|
算法 Serverless C++
用同一个哈希表实现unordered_map和unordered_set(C++实现)【STL】
用同一个哈希表实现unordered_map和unordered_set(C++实现)【STL】
15 0
|
2月前
|
存储 编译器 Serverless
用C++实现一个哈希桶并封装实现 unordered_map 和 unordered_set
用C++实现一个哈希桶并封装实现 unordered_map 和 unordered_set
|
6月前
|
存储 Java C++
unordered_set和unordered_map的使用【STL】
unordered_set和unordered_map的使用【STL】
139 0
|
11月前
|
存储 算法 编译器
【C++】开散列哈希表封装实现unordered_map和unordered_set
【C++】开散列哈希表封装实现unordered_map和unordered_set
|
11月前
|
存储 自然语言处理 C++
【STL】set、map的使用介绍
用来表示具有一一对应关系的一种数据结构,该结构中只包含两个成员变量key和value,key代表关键字,value表示关键字对应的值。比如:现在要建立一个英译汉的词典,那该词典中必然有英文单词和与其对应的中文含义,而且,英文单词与中文含义是一一对应的关系,
|
存储 C++ 容器
【C++进阶】十、用哈希表对unordered_set和unordered_map进行封装
目录 一、改造哈希表 1.1 节点定义 1.2 哈希表迭代器相关 1.3 哈希表接口相关 二、unordered_set模拟实现代码 三、unordered_map模拟实现代码
63 0
【C++进阶】十、用哈希表对unordered_set和unordered_map进行封装
|
存储 算法 Serverless
【C++】哈希表 | 闭散列 | 开散列 | unordered_map 和 unordered_set 的模拟实现(下)
【C++】哈希表 | 闭散列 | 开散列 | unordered_map 和 unordered_set 的模拟实现(下)
【C++】哈希表 | 闭散列 | 开散列 | unordered_map 和 unordered_set 的模拟实现(下)
|
存储 C++ 容器
【C++】哈希表 | 闭散列 | 开散列 | unordered_map 和 unordered_set 的模拟实现(上)
【C++】哈希表 | 闭散列 | 开散列 | unordered_map 和 unordered_set 的模拟实现(上)
【C++】哈希表 | 闭散列 | 开散列 | unordered_map 和 unordered_set 的模拟实现(上)
|
C++ 容器
【C++】-- STL之unordered_map/unordered_set详解(三)
【C++】-- STL之unordered_map/unordered_set详解
【C++】-- STL之unordered_map/unordered_set详解(三)
|
存储 C++ 容器
【C++】-- STL之unordered_map/unordered_set详解(二)
【C++】-- STL之unordered_map/unordered_set详解
【C++】-- STL之unordered_map/unordered_set详解(二)