STL无序容器之unordered_map
来源:互联网 发布:哈登15 16赛季数据 编辑:程序博客网 时间:2024/04/24 09:04
一、概述
头文件<unordered_map>定义了unordered_map和unordered_multimap容器。这个是C++11新特性,以前的编译器不支持。unordered_map类模版定义如下:
template < class Key, // unordered_map::key_type class T, // unordered_map::mapped_type class Hash = hash<Key>, // unordered_map::hasher class Pred = equal_to<Key>, // unordered_map::key_equal class Alloc = allocator< pair<const Key,T> > //unordered_map::allocator_type > class unordered_map;
其中Key是map的键值类型,T是mapped value的值类型,Hash是容器底层存储用的哈希函数对象,Pred是判断键值是否相等的bool函数,Alloc是内存空间配置器。
在unordered_map中,键值可以唯一确定一个与之相关联的数值。unordered_map中的元素存储既不以key为序,也不以mapped_value为序,但是都依据其hash值存储在桶里。unordered_map存取单个元素要比map更快。
二、成员函数
关于unordered_map的说明及成员函数的使用,参见C++参考手册。
三、示例代码
//hash_map的使用#include<iostream>#include<hash_map>#include<cstring>using namespace std;struct eqstr{ bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) == 0; }};int main(){ hash_map<const char*, int> days; days["january"] = 31; days["february"] = 28; days["march"] = 31; days["april"] = 30; days["may"] = 31; days["june"] = 30; days["july"] = 31; days["august"] = 31; days["september"] = 30; days["october"] = 31; days["november"] = 30; days["december"] = 31; cout << "september -> " << days["september"] << endl; cout << "june -> " << days["june"] << endl; cout << "february -> " << days["february"] << endl; cout << "december -> " << days["december"] << endl; hash_map<const char*, int>::iterator ite1 = days.begin(); hash_map<const char*, int>::iterator ite2 = days.end(); for (; ite1 != ite2; ++ite1) cout << ite1->first << ' '; cout << endl; if (days.find("january") != days.end()) cout << "january has " << days.find("january")->second << "days!" << endl; return 0;}
实验结果:
1 0
- STL无序容器之unordered_map
- STL::unordered_map之无序map
- STL::unordered_map之无序map
- C++11 unordered_map 无序容器的使用
- STL中的无序容器
- 容器类之 unordered_map
- STL之unordered_map
- 标准模板库(STL)之无序容器列传
- C++ STL之map与unordered_map
- c++11 无序map unordered_map
- 无序容器
- 无序容器
- STL学习笔记— —无序容器(Unordered Container)
- STL容器之顺序容器
- STL容器之关联容器
- STL容器之串行容器
- STL容器之关联容器
- TR1 五种新容器 之 unordered_set unordered_multiset unordered_map unordered_multimap
- __initialize()和类的构造函数__construct()
- [译]Android Weekly 250期优秀库与代码
- Python工具-pdfkit
- 使用Docker容器来源码编译etcd
- LeetCode#43. Multiply Strings
- STL无序容器之unordered_map
- j_link管脚定义
- php 框架选择(我对各框架的了解还不够深入,后续再完善)
- dwz IE兼容问题之图片回显与下拉滚动条消失
- leetcode134. Gas Station
- 好玩的editText
- 设计模式——工厂模式
- Unity中的Path对应各平台中的Path
- 数据分析随记