目录
简介:
所有元素都会在插入时自动被排序
本质:
set/multiset属于关联式容器,底层结构是用二叉树实现。
set和multiset区别:
set不允许容器中有重复的元素
multiset允许容器中有重复的元素
功能:创建set容器以及赋值
构造:

赋值:

示例:
- #include
- using namespace std;
- #include
-
- 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;
-
- //插入数据 只有insert方式
- s1.insert(20);
- s1.insert(10);
- s1.insert(40);
- s1.insert(30);
- s1.insert(30);
-
- printSet(s1);
- }
-
- int main() {
- test01();
- system("pause");
- return 0;
- }
结果:(结果是排好序的,而且无重复元素)

拷贝构造:
- set<int> s2(s1);
- printSet(s2);
赋值:
- set<int> s3;
- s3 = s2;
- printSet(s3);
功能:
统计set容器大小以及交换set容器
函数原型:

元素数目:
- set<int> s1;
- s1.size();
是否为空:
- if (s1.empty())
- {
- }
- else
- {
- }
交换:
s1.swap(s2);
功能:set容器进行插入数据和删除数据
函数原型:

示例:erase删除索引为1的位置上的元素:
- set<int>::iterator it = s1.begin();
- it++;
- s1.erase(it);
结果:

功能:对set容器进行查找数据以及统计数据
函数原型:

示例:(find)
- #include
- using namespace std;
- #include
-
- void test01()
- {
- set<int> s1;
-
- s1.insert(30);
- s1.insert(40);
- s1.insert(10);
- s1.insert(20);
-
- set<int>::iterator pos = s1.find(30);
- if (pos != s1.end())
- {
- cout << "找到元素:" << *pos << endl;
- }
- else
- {
- cout << "未找到元素" << endl;
- }
-
- }
-
- int main() {
- test01();
- system("pause");
- return 0;
- }
结果:

统计元素个数:(有为1,没有为0)
- int num = s1.count(20);
- cout << num << endl;
1.set不能插入重复数据,而multiset可以
2.set插入数据的同时会返回插入结果,表示插入是否成功
3.multiset不会检测数据,因此可以插入重复数据
示例1:(判断set是否插入成功)
- #include
- using namespace std;
- #include
-
- //检查是否插入成功
- void check(pair
int >::iterator, bool> &ret) - {
- if (ret.second)
- {
- cout << "插入成功" << endl;
- }
- else
- {
- cout << "插入失败" << endl;
- }
- }
-
- void test01()
- {
- set<int> s;
-
- pair
int>::iterator, bool> ret = s.insert(10); - check(ret);
-
- //再次插入相同的元素
- ret = s.insert(10);
- check(ret);
- }
-
- int main() {
- test01();
- system("pause");
- return 0;
- }
结果:

multiset允许插入重复数据:
- multiset<int> ms;
-
- //multiset允许重复插入相同数据
- ms.insert(10);
- ms.insert(10);
功能:成对出现的数据,利用对组可以返回两个数据
两种创建方式:

示例:
- #include
- using namespace std;
- #include
-
- void test01()
- {
- //第一种创建方式
- pair
int > p1("Tom", 20); - cout << "姓名:" << p1.first << "年龄:" << p1.second << endl;
-
- //第二种创建方式
- pair
int> p2 = make_pair("Jerry",30); - cout << "姓名:" << p2.first << "年龄:" << p2.second << endl;
- }
-
- int main() {
- test01();
- system("pause");
- return 0;
- }
结果:

学习目标:
set容器默认排序规则为从小到大,掌握如何改变排序规则
主要技术点:
利用仿函数,可以改变排序规则
示例1:(set存放内置数据类型,让set的排序规则为从大到小)
- #include
- using namespace std;
- #include
-
- class MyCompare
- {
- public:
- bool operator()(int v1, int v2)
- {
- return v1 > v2;
- }
- };
-
- void test01()
- {
- //指定排序规则为从大到小
- set<int, MyCompare> s1;
- s1.insert(10);
- s1.insert(40);
- s1.insert(20);
- s1.insert(50);
- s1.insert(30);
-
- //打印s1
- for (set<int, MyCompare>::iterator it = s1.begin(); it != s1.end(); it++)
- {
- cout << *it << " ";
- }
- cout << endl;
- }
-
- int main() {
- test01();
- system("pause");
- return 0;
- }
示例2:(set存放自定义数据类型,让set按照年龄降序)
- #include
- using namespace std;
- #include
- #include
-
- //自定义数据类型
- 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)
- {
- //按照年龄降序
- return p1.m_Age > p2.m_Age;
- }
- };
-
- void test01()
- {
- set
s; -
- //创建Person对象
- Person p1("Tom", 18);
- Person p2("Nick", 20);
- Person p3("Jerry", 16);
- Person p4("Flynt", 21);
-
- s.insert(p1);
- s.insert(p2);
- s.insert(p3);
- s.insert(p4);
-
- for (set
::iterator it = s.begin(); it != s.end(); it++) - {
- cout << "姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;
- }
- }
-
- int main() {
- test01();
- system("pause");
- return 0;
- }
结果:
