当前位置:网站首页>STL教程7-set、pair对组和仿函数
STL教程7-set、pair对组和仿函数
2022-06-30 15:48:00 【贪睡的蜗牛】
一、set容器
- set和multiset是一个集合容器,其中set所包含的元素是唯一的,集合中的元素按一定的顺序自动排列。set采用红黑树变体的数据结构实现,红黑树属于平衡二叉树。在插入操作和删除操作上比vector快,在n个数中查找目标数的效率是 log 2 n
- set容器中不允许重复元素(中文名集合的意思,只能有一个),multiset允许重复元素。
- 只提供Insert方法初始化
- set/multiset的特性是所有元素会根据元素的值自动进行排序
- set不能修改某个值,如果想修改只能删除再添加
1、set构造函数
set<int> s1;//自动进行排序,默认从小到大。
s1.insert(7);
s1.insert(2);
s1.insert(3);//这时候打印时会输出 2 3 7,已经自动排序了
2、set赋值操作
set<int> s2;
s2 = s1;
s2.swap(s1);//swap交换集合容器
3、set大小操作
size();//获得容器元素的数目
empty();//判断内容器是否为空
4、set插入和删除操作
insert(elem);//在容器中插入元素
clear();//清除所有元素
erase(pos);//删除迭代器指的元素,下面这两个都是迭代器
erase(beg,end);//删除区间的元素
erase(elem);//删除容器中值为elem的元素,是删除一个,虽然只有一个
5、set的查找(重要)
s1.find(4)找4位置的迭代器
lower_bound(2) 找第一个大于等于2的(元素)迭代器值
s1.upper_bound(2); 找第一个大于K的值
set<int> s1;
//插入几个元素
s1.insert(7);
s1.insert(2);
s1.insert(4);
s1.insert(5);
s1.insert(1);
s1.insert(9);
//使用迭代器从头开始查找元素
//find函数查找元素,返回元素位置的迭代器,如果没有找到返回end()的迭代器
set<int>::iterator ret = s1.find(4);
//下面判断元素是否找到
if (ret == s1.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "ret:" << *ret << endl;
}
//---------------------------------------------------------------
//lower_bound(2)找第一个大于等于k的(元素)迭代器值
ret = s1.lower_bound(2);
if (ret == s1.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "ret:" << *ret << endl;
}
//---------------------------------------------------------------
//找第一个大于K的值
ret = s1.upper_bound(2);
if (ret == s1.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "ret:" << *ret << endl;
}
//equal_range 返回Lower_bound 和 upper_bound值
pair<set<int>::iterator, set<int>::iterator> myret = s1.equal_range(2);
//这里用到了pair我们在下面补充对组的相关内容。
/*myret.first; myret.second;*/
if (myret.first == s1.end())
{
cout << "can't find" << endl;
}
else
{
cout << "myret:" << *myret.first << endl;//返回Lower_bound
}
if (myret.second == s1.end())
{
cout << "can't find" << endl;
}
else
{
cout << "myret:" << *myret.second << endl; //返回upper_bound
}
6、修改set的排序次序
看下面的仿函数
set<int, mycompare> s3;//这里使用仿函数后,在插入时会修改默认的插入顺序
s3.insert(7);
s3.insert(2);
s3.insert(4);
s3.insert(5);
s3.insert(1);
s3.insert(9);
7、set里添加对象
set添加元素需要排序,但如果直接排序,set不知道怎么排会报错,因此也需要一个仿函数
//定义一个类
class Person
{
public:
Person(int age, int id):id(id),age(age){
}
public:
int id;
int age;
};
//定义一个仿函数,根据对象的年龄大小排序
class mycompare2
{
public:
bool operator()(Person p1, Person p2)const
{
return p1.age > p2.age;
}
};
void test(){
//使用仿函数进行自定义排序
set<Person,mycompare2> sp;
Person p1(10, 20), p2(30, 40), p3(50, 60);
sp.insert(p1);
sp.insert(p2);
sp.insert(p3);
}
需要注意的是,如果使用find,这时候应该查找比较的那个元素,比如上面用age进行比较的,那么查找的时候也是用这个age去查找,这个age就相当于键了。比如下面代码,虽然没有加入p4,但是p4里的40是已经加入的,因此可以找到
Person p4(10, 40);
auto ret = sp.find(p4);//set<Person,mycompare2>::iterator
if (ret == sp.end())
{
cout << "can't find" << endl;
}
else
{
cout << "find:" << (*ret).id << " " << (*ret).age << endl;
}
二、pair
pair是将2个数据组合成一组数据,当需要这样的需求时就可以使用pair,如stl中的map就是将key和value放在一起来保存。另一个应用是,当一个函数需要返回2个数据的时候,可以选择pair。
//法一
pair<string, int> pair1(string("number"), 20);
cout << pair1.first << " " << pair1.second << endl;
//或者pair<string,int> pair2=make_pair("name",30)
pair<string, int> pair2 = make_pair("name", 30);
cout << pair2.first << " " << pair2.second << endl;
//pair赋值
pair<string, int> pair3 = pair2;
cout << pair2.first << " " << pair2.second << endl;
三、仿函数
//仿函数
class mycompare
{
public:
bool operator()(int v1,int v2)const
{
return v1 > v2;
}
};
边栏推荐
- Rong Lianyun launched rphone based on Tongxin UOS to create a new ecology of localization contact center
- 赛芯电子冲刺科创板:拟募资6.2亿 实控人谭健为美国籍
- Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world
- Yunhe enmo won the bid for Oracle maintenance project of Tianjin Binhai rural commercial bank in 2022-2023
- Unsupported major. minor version 52.0
- Halcon knowledge: regional topics [07]
- 边缘计算平台如何助力物联网发展
- 构建适合组织的云原生可观测性能力
- 云和恩墨中标天津滨海农村商业银行2022-2023年度Oracle维保项目
- The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer
猜你喜欢

24:第三章:开发通行证服务:7:自定义异常(来表征程序中出现的错误);创建GraceExceptionHandler来全局统一处理异常(根据异常信息,构建对应的API统一返回对象的,JSON数据);

15年做糊21款硬件,谷歌到底栽在哪儿?

荣盛生物冲刺科创板:拟募资12.5亿 年营收2.6亿

7 月 2 日邀你来TD Hero 线上发布会

RT thread heap size setting

MySQL transaction / lock / log summary

MC Instruction Decoder

Niuke.com: minimum cost of climbing stairs

抖快B为啥做不好综艺
Two methods for MySQL to open remote connection permission
随机推荐
MySQL transaction / lock / log summary
Li Zexiang, a legendary Chinese professor, is making unicorns in batches
Unsupported major. minor version 52.0
RT thread heap size setting
今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计
CMakeLists 基础
[cve-2019-0193] - Apache Solr dataimport remote command execution analysis
Three development trends of enterprise application viewed from the third technological revolution
几百行代码实现一个 JSON 解析器
24:第三章:开发通行证服务:7:自定义异常(来表征程序中出现的错误);创建GraceExceptionHandler来全局统一处理异常(根据异常信息,构建对应的API统一返回对象的,JSON数据);
如何得到股票开户的优惠活动?在线开户安全么?
Distributed machine learning: model average Ma and elastic average easgd (pyspark)
'&lt;', Hexadecimal value 0x3c, is an invalid problem solving
华为帐号多端协同,打造美好互联生活
牛客网:有多少个不同的二叉搜索树
【机器学习】K-means聚类分析
更多龙蜥自研特性!生产可用的 Anolis OS 8.6 正式发布
Halcon knowledge: regional topics [07]
Interesting research on mouse pointer interaction
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)