STL(十六)hash_map哈希映照容器
来源:互联网 发布:芭碧琪面膜怎么样 知乎 编辑:程序博客网 时间:2024/06/10 16:35
hash_map哈希映照容器与map映照容器比较相似,都将记录型的元素划分为键值和映照数据两个部分,然后根据键值的大小,将键值不重复的元素插入容器。不同之处在于,hash_map使用哈希表的数据结构,map使用红黑树的数据结构。对于元素的检索来说,这两种数据结构都有快速的检索效率。hash_map检索时使用的键值比较次数少,容器需占用较多的空间,用迭代器遍历出来的元素是非排序的。map则使用链表的二分法进行检索,空间使用率高,用迭代器遍历出来的元素是排序的,而且可提供反向迭代器。
一、hash_map技术原理
二、hash_map应用基础
#include <hash_map>
1、创建hash_map对象
hash_map()
hash_map(size_type n)
hash_map(size_type n, const hasher &h)
hash_map(size_type n, const hasher &h, const key_equal &k)
hash_map(const hash_set &)
2、元素插入
pair<iterator,bool> insert(const value_type &v)
void insert(InputIterator first, InputIterator last)
3、元素的删除
void erase(iterator position)
size_type erase(const key_type &k)
void erase(iterator first, iterator last)
void clear()
4、元素的遍历访问
iterator begin()
iterator end()
#include <iostream>#include <hash_map>template<class Key, class NameType, class YearType, class AddrType>struct StudentRecord_tag { //学生记录结构体 struct StudentInfo_tag { //学生信息结构体 NameType name; YearType year; AddrType addr; }; // typedef Key IdType; typedef StudentInfo_tag StudentInfo; // IdType id; //学号 StudentInfo sf; //学生信息,};int main(void) { using namespace std; typedef StudentRecord_tag<int, char*, int, char*> StudentRecord; StudentRecord srArray[] = { //3笔学生记录 {192, "李强", 21, "北京" }, {193, "王文", 29, "上海" }, {191, "张三", 38, "深圳" } }; // hash_map<StudentRecord::IdType, StudentRecord::StudentInfo> hm; // for (int j=0; j<3; j++) { hm[srArray[j].id] = srArray[j].sf; } //迭代器遍历元素 hash_map<StudentRecord::IdType, StudentRecord::StudentInfo>::iterator i, iend; iend = hm.end(); cout << "学号 " << "姓名 " << "年龄 " << "地址 " << endl; for (i=hm.begin(); i!=iend; i++) { cout << (*i).first << ' ' << (*i).second.name << ' ' << (*i).second.year << ' ' << (*i).second.addr << ' ' << endl; } return 0;}
5、元素的搜索
iterator find(const key_type &k) const
6、其他的常用函数
- STL(十六)hash_map哈希映照容器
- hash_map哈希映照容器的实现(template)
- C++ STL学习笔记十二 hash_map映照容器
- C++ STL学习笔记十二 hash_map映照容器
- C++ STL学习笔记十二 hash_map映照容器
- STL(十三)map映照容器
- STL(十四)multimap多重映照容器
- STL容器(四)——hash_map
- STL泛型编程-map映照容器
- STL 之 multimap 多重映照容器
- C++ STL--map映照容器学习笔记
- C++STL之map映照容器
- C++STL之multimap多重映照容器
- 【STL】关联容器 — hash_map
- C++ STL学习笔记九 map映照容器
- C++ STL学习笔记十 multimap多重映照容器
- STL泛型编程-multimap多重映照容器
- STL泛型编程学习之Map映照容器
- 常用的加密算法
- 4node博客项目系列静态文件的托管
- 计算机类中文核心期刊简介
- 怎样用Python3 写一个爬图片的程序?
- 洛谷P3383 线性筛素数(线性筛)
- STL(十六)hash_map哈希映照容器
- netty(十七)源码分析之客户端创建
- 插入排序及归并排序java代码实现及详细注释
- Git命令上传和克隆工程
- scala类参数修饰符浅析
- MyBatis 01
- FreeType之字形分析(一)
- 堆
- POJ1700Crossing River(过河问题)