当前位置:网站首页>STL entry basics map and set containers
STL entry basics map and set containers
2022-08-02 03:16:00 【little saint programming】
目录
STL是C++标准库的重要组成部分,map和set容器在实际中非常的重要,Relational container storage structure of key/value pair<key,value>,这在数据检索时比序列式容器效率更高
The tree associated type container
树型结构的关联式容器主要有四种:map、set、multimap、multiset,They are using the red-black tree as its underlying,容器中的元素是一个有序的序列
set容器的接口使用
set和multiset容器的插入
//set插入 排序+去重
void test_set1()
{
set<int> s;
s.insert(5);
s.insert(6);
s.insert(2);
s.insert(4);
s.insert(3);
s.insert(2);
s.insert(5);
set<int>::iterator it = s.begin();
while (it != s.end())
{
//*it = 10;不支持修改
cout << *it << " ";//打印2 3 4 5 6 Out of order+去重
++it;
}
cout << endl;
}
//multiset 排序+不去重
void test_set5()
{
multiset<int> s;
s.insert(5);
s.insert(6);
s.insert(2);
s.insert(4);
s.insert(3);
s.insert(2);
s.insert(5);
set<int>::iterator it = s.begin();
while (it != s.end())
{
//*it = 10;不支持修改
cout << *it << " ";//打印2 2 3 4 5 5 6 Out of order+不去重
++it;
}
cout << endl;
cout << s.count(5) << endl;//5出现的次数
cout << s.erase(5) << endl;//删除所有5 Return to delete a few
for (auto e : s)
{
cout << e << " ";//打印2 2 3 4 6
}
cout << endl;
}set容器的查找
//find 返回迭代器 不能修改
void test_set2()
{
set<int> s;
s.insert(5);
s.insert(6);
s.insert(2);
s.insert(4);
set<int>::iterator pos = s.find(2);//0(logN)
if (pos != s.end())//set本身的find
{
cout << "set.find找到了" << endl;
}
pos = find(s.begin(), s.end(), 2);//0(N)
if (pos != s.end())//Algorithm offind
{
cout << "find找到了" << endl;
}
}set容器的删除
//删除数据
void test_set3()
{
set<int> s;
s.insert(5);
s.insert(6);
s.insert(2);
s.insert(4);
s.insert(6);
s.insert(9);
int x;
while (cin >> x)//持续输入
{
set<int>::iterator pos = s.find(x);
if (pos != s.end())
{
s.erase(pos);
cout << "删除" << x << "成功" << endl;
}
else
{
cout << x << "不在set中" << endl;
}
for (auto e : s)
{
cout << e << " ";
}
cout << endl;
}
}lower_bound和upper_bound
//lower_bound 返回>=位置的迭代器
//upper_bound 返回>位置的迭代器
void test_set4()
{
set<int> s;
s.insert(5);
s.insert(6);
s.insert(2);
s.insert(4);
s.insert(9);
for (auto e : s)
{
cout << e << " ";
}
cout << endl;
//删除x<=[]<=y的区间
int x, y;
cin >> x >> y;
auto left = s.lower_bound(x);
auto right = s.upper_bound(y);//
s.erase(left,right);
for (auto e : s)
{
cout << e << " ";
}
cout << endl;
}map容器的接口使用
Key/value pair generally contains only two member variableskey和value,key代表键值,value表示与key对应的信息,Such as the dictionary for middle school students(key)对应student(value)
SGI-STL中关于键值对的定义:
template <class T1, class T2>
struct pair
{
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair(): first(T1()), second(T2())
{}
pair(const T1& a, const T2& b): first(a), second(b)
{}
};
map容器的初始化
void test_map1()
{
map<string, string>dict;
//调用pair 构造函数
dict.insert(pair<string, string>("sort", "排序"));
//构造函数
pair<string, string>kv("insert", "插入");
dict.insert(kv);
//函数模板
dict.insert(make_pair("left", "左边"));
dict.insert(make_pair("left", "剩余"));
}map容器的遍历
//用mapSave a small dictionary
void test_map1()
{
map<string, string>dict;
//调用pair 构造函数
dict.insert(pair<string, string>("sort", "排序"));
//构造函数
pair<string, string>kv("insert", "插入");
dict.insert(kv);
//函数模板
dict.insert(make_pair("left", "左边"));
dict.insert(make_pair("left", "剩余"));
//The iterator traversal traversal
//map<string, string>::iterator it = dict.begin();
auto it = dict.begin();
while (it != dict.end())
{
//pair (将key和value打包)
cout << it->first << ":" << it->second << endl;
++it;
}
cout << endl;
//auto 遍历
for (auto& kv : dict)
{
cout << kv.first << ":" << kv.second << endl;
}
}map容器的查找
//map支持[]操作符,在operator[]中实际进行插入查找
//The number of statistics below each word
void test_map2()
{
string arr[]{ "大学","中庸","论语","孟子","孔子"
,"大学","史记","论语" };
map<string, int>countMap;
for (auto& str : arr)
{
countMap[str]++;//统计次数 []返回的是value的引用
//查找+修改 插入+修改
}
for (auto& kv : countMap)
{
cout << kv.first << ":" << kv.second << endl;
}
}operator[]的原理是:
用<key, T()>构造一个键值对,然后调用insert()函数将该键值对插入到map中
如果key已经存在,插入失败,insert函数返回该key所在位置的迭代器
如果key不存在,插入成功,insert函数返回新插入元素所在位置的迭代器
operator[]函数最后将insert返回值键值对中的value返回
学习set和map一定要学会查看文档,这里放一个官方文档链接map文档和set文档,set和map在实际中非常的重要,我们只要熟悉常见的接口就可以
希望这篇文章大家有所收获,我们下篇见
边栏推荐
猜你喜欢

mysql使用on duplicate key update批量更新数据

MySQL8--Windows下使用压缩包安装的方法

"Paid paddling" stealthily brushes Brother Ali's face scriptures, challenges bytes three times, and finally achieves positive results

MySQL8.0.28安装教程

Daily practice------There are n integers, so that the previous numbers are moved back m positions in order, and the last m numbers become the first m numbers

JSP WebSehll 后门脚本

Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)

Small program (necessary common sense for development) 1

Keil development environment installation tutorial

mysql8.0.28 download and installation detailed tutorial, suitable for win11
随机推荐
R16 Type II量化反馈码本的产生
MySQL中的时区设置
基于可逆网络的单一图像超分辨率
dropout
5.nodejs--cross domain, CORS, JSONP, Proxy
关于#sql#的问题:该怎么写sql语句,
蓝鲸DevOps荣获平台类工具企业选择率第一
Lua安装及常用命令使用
TRICK second bullet
【遥控器开发基础教程3】疯壳·开源编队无人机-ADC(摇杆控制)
mysql8.0.28下载和安装详细教程,适配win11
合奥科技网络 面试(含参考答案)
【LeetCode】20. Valid parentheses
EF Core:基于关系的复杂查询 区分IEnumerable和IQueryable
第 304 场力扣周赛
(Repost) HashCode Summary (1)
7、MySQL Workbench 导出导入数据库
5.合宙Air32F103_LCD_key
“带薪划水”偷刷阿里老哥的面经宝典,三次挑战字节,终成正果
深度学习:目标检测入门知识