当前位置:网站首页>第十三章 STL 之 set/ multiset
第十三章 STL 之 set/ multiset
2022-07-17 10:23:00 【qq_43205256】
1 set基本概念
2 set构造和赋值
#include <set>
void printSet(set<int> & s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//构造和赋值
void test01()
{
set<int> s1;
s1.insert(20);
s1.insert(10);
s1.insert(30);
s1.insert(40);
printSet(s1);
//拷贝构造
set<int>s2(s1);
printSet(s2);
//赋值
set<int>s3;
s3 = s2;
printSet(s3);
}
int main()
{
test01();
return 0;
}输出结果:
10 20 30 40
10 20 30 40
10 20 30 403 set大小和交换
#include <set>
void printSet(set<int> & s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " "; }cout << endl;
}
//大小
void test01()
{
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
if (s1.empty())
{
cout << "s1为空" << endl;
}
else
{
cout << "s1不为空" << endl;
cout << "s1的大小为: " << s1.size() << endl;
}
}
//交换
void test02()
{
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
set<int> s2;
s2.insert(100);
s2.insert(300);
s2.insert(200);
s2.insert(400);
cout << "交换前" << endl;
printSet(s1);
printSet(s2);
cout << endl;
cout << "交换后" << endl;
s1.swap(s2);
printSet(s1);
printSet(s2);
}
int main()
{
test01();
test02();
return 0;
}输出结果:
s1不为空
s1的大小为: 4
交换前
10 20 30 40
100 200 300 400
交换后
100 200 300 400
10 20 30 404 set插入和删除
#include <set>
void printSet(set<int> & s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//插入和删除
void test01()
{
set<int> s1;
//插入
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
printSet(s1);
//删除
s1.erase(s1.begin());
printSet(s1);
s1.erase(30);
printSet(s1);
//清空
s1.erase(s1.begin(), s1.end());
printSet(s1);
s1.clear();
printSet(s1);
}
int main()
{
test01();
return 0;
}输出结果:
10 20 30 40
20 30 40
20 405 set查找和统计
#include <set>
//查找和统计
void test01()
{
set<int> s1;
//插入
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
//查找
set<int>::iterator pos = s1.find(30);
if (pos != s1.end())
{
cout << "找到了元素 : " << *pos << endl;
}
else
{
cout << "未找到元素" << endl;
}
//统计
int num = s1.count(30);
cout << "num = " << num << endl;
}
int main()
{
test01();
return 0;
}输出结果:
找到了元素 : 30
num = 16 set和multiset区别
#include <set>
//set和multiset区别
void test01()
{
set<int> s;
pair<set<int>::iterator, bool> ret = s.insert(10);
if (ret.second)
{
cout << "第一次插入成功!" << endl;
}
else
{
cout << "第一次插入失败!" << endl;
}
ret = s.insert(10);
if (ret.second)
{
cout << "第二次插入成功!" << endl;
}
else
{
cout << "第二次插入失败!" << endl;
}
//multiset
multiset<int> ms;
ms.insert(10);
ms.insert(10);
for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
return 0;
}输出结果:
第一次插入成功!
第二次插入失败!
10 107 pair对组创建
#include <string>
//对组创建
void test01()
{
pair<string, int> p(string("Tom"), 20);
cout << "姓名: " << p.first << " 年龄: " << p.second << endl;
pair<string, int> p2 = make_pair("Jerry", 10);
cout << "姓名: " << p2.first << " 年龄: " << p2.second << endl;
}
int main()
{
test01();
return 0;
}输出结果:
姓名: Tom 年龄: 20
姓名: Jerry 年龄: 10总结:
8 set容器排序
#include <set>
class MyCompare
{
public:
bool operator()(int v1, int v2)const
{
return v1 > v2;
}
};
void test01()
{
set<int> s1;
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(30);
s1.insert(50);
//默认从小到大
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//指定排序规则
set<int, MyCompare> s2;
s2.insert(10);
s2.insert(40);
s2.insert(20);
s2.insert(30);
s2.insert(50);
for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
return 0;
}
#include <string>
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class comparePerson
{
public:
bool operator()(const Person& p1, const Person& p2)const
{
//按照年龄进行排序 降序
return p1.m_Age > p2.m_Age;
}
};
void test01()
{
set<Person, comparePerson> s;
Person p1("刘备", 23);
Person p2("关羽", 27);
Person p3("张飞", 25);
Person p4("赵云", 21);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
for (set<Person, comparePerson>::iterator it = s.begin(); it != s.end(); it++)
{
cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age << endl;
}
}
int main()
{
test01();
return 0;
}
输出结果:
姓名: 关羽 年龄: 27
姓名: 张飞 年龄: 25
姓名: 刘备 年龄: 23
姓名: 赵云 年龄: 21边栏推荐
猜你喜欢

KNN分类器

Log desensitization - Reference

KNN classifier

焱融科技入选北京市 2022 年度“专精特新”,领航混合云文件存储

Google play app store may delete the overview of APP permissions and use a new combination of data security information

565. Array nesting
![[troubleshooting] common problems and solutions when installing MySQL in Windows system](/img/45/339bda7ecf8879999d8ff128c1d3ab.png)
[troubleshooting] common problems and solutions when installing MySQL in Windows system

Latest fruit flstudio20.9 low version upgrade high version tutorial

Markdown (5): anchor link

第一部分—C语言基础篇_1. C语言概述
随机推荐
开发第一个Flink应用
Codeworks 5 questions per day (average 1500) - day 17
Anaconda与Jupyter Notebook入门级详细使用教程
SharePoint接入简要笔记
将视频格式转换为gif图片格式
Daily model series: July 11, 2022
每日模型系列:2022.07.11
Towhee daily model weekly report
L1-088 静静的推荐(测试点1)
研究发现DNA纳米设备注射液可安全用于医疗用途
The inflection point of eth may be just around the corner, which is how to
日志脱敏-参考
DEDECMS织梦保存当前栏目更改时失败的解决方法
v-mode
05---增透膜
Classification of top essays
[Network Research Institute] the threat of machine learning system is time to take it seriously
代码随想录:刷题记录(更新中)
How is MySQL data stored on disk?
Encapsulation API, request interception, response interception, authentication timeliness