哈希表
哈希表在使用层面上可以理解为一种集合结构;也可以叫做无序表
如果只有key,没有伴随数据value,可以使用hashset结构,C++中叫做unordered_set;
如果既有key,也有value,可以使用hashmap结构,C++中叫做unordered_map;
哈希表的各种操作
- 增/改
- 删除
- 查找
增删改查的时间都是常数级别O(1)的,与数据量无关
哈希表在存储数据的时候,若储存的数据类型是基本类型,哈希表是按值来传递的,就是说哈希表直接储存这个基本类型的数据,不管这个数据有多大,比如一个string类型的数据”sdfdgf“;哈希表里边开辟一个内存空间储存这个string数据
如果储存的数据类型不是基本类型,而是一些自定义类型,比如自己定义可一个Student类,这时候哈希表里边一个八字节的空间位置这个student类数据的内存地址;
有序表
哈希表也可以叫做无序表,所以哈希表的操作(增删改查)有序表都支持;
#include<vector>
#include<iostream>
#include<math.h>
#include<climits>
#include<set>
using namespace std;
int main()
{
set<int> S;
S.insert(4);
S.insert(3);
S.insert(5);
S.insert(1);
cout<<*S.begin()<<endl;//有序表中最小值
cout<<*S.end()<<endl;//有序表中的最大值
cout<<S.size()<<endl;
cout<<S.max_size() <<endl;
//returns an iterator to the first element not less than the given key
cout<<*S.lower_bound(2)<<endl;
//returns an iterator to the first element greater than the given key
cout<<*S.upper_bound(4)<<endl;
return 0;
}