当前位置:网站首页>STL map
STL map
2022-07-22 05:57:00 【joker_0030】
#include <iostream>
#include <map>
#include<algorithm>
using namespace std;
void fun(pair<int, char> pr)
{
cout << pr.first << pr.second << endl;//first代表键值,second代表实际值。
}
void MapConstruct()
{
//重命名pair。
typedef pair<int, char> in_pair;
//迭代函数,用于检测当map有相同键值时,实际值是否插入成功。
pair<map<int, char>::iterator, bool> pr;//
//定义对象,并赋值。
map<int, char> db;//<键值,实际值>
db.insert(pair<int, char>(1, 'a'));//需要和map的类型一一对应。
pr=db.insert(in_pair(2, 'b'));
cout << pr.second << endl;//用于检测当map有相同键值时,实际值是否插入成功。当插入成功,则返回1,否则返回0。
pr=db.insert(in_pair(2, 'c'));
cout << pr.second << endl;//用于检测当map有相同键值时,实际值是否插入成功。
//输出。
for_each(db.begin(),db.end(),fun);
}
int main()
{
MapConstruct();
system("pause");
return 0;
}
*1、map:有key值与实际值,key值不允许重复;底层数据结构:红黑树(平衡 排序二叉树);特点:键值保存,高效访问,x=log2^N(插入和查找的时间复杂度);1、x为查找次数,2、N为数据总量,2^x>=N。
2、multimap:有key值与实际值,key值允许重复。
3、set:只有key值或实际值,或者说key值就是实际值,只存在一项, 且不允许重复。
4、multset:只有key值或实际值,或者说key值就是实际值,只存在一项, 允许重复。
边栏推荐
猜你喜欢
随机推荐
MVC模式和三层架构
UE4 key to open the door
final、finally、finalize的区别
ig,ax = plt.subplots()
Application of tensorflow optimizers in iris classification task
Is there anyone who can't analyze these data cases? A trick to teach you how to visualize recruitment data~
Win11终端管理员打不开解决方法
写一个定序器插件 sequence 字幕(一)
接招吧。最强“高并发”系统设计 46 连问,分分钟秒杀一众面试者
Polygon chain matic concept and underlying mechanism
IO流的分类和方法
Application of workflow engine in vivo marketing automation | engine 03
14 multithread 1
Ffmpeg-rk3399 ffplay learning analysis
Blob URL DataURL
LVS load balancing cluster
-bash-4.2$
JWT学习
15_ Additional models
利用二分法查找数组的元素