目录
一、set 简介
C++ 中,std::set
和multiset
都属于关联式容器,且都包含在头文件#include <set>
中,其底层结构都是用二叉树实现的,该类容器中元素在插入时都会被自动排序。
二、set 构造函数
构造函数原型 | 解释 | |
---|---|---|
1 | set<T> st | 默认构造 |
2 | set(const set &st) | 拷贝构造 |
三、set 赋值操作
函数原型:= | 解释 | |
---|---|---|
1 | set& operator=(const set &st) | 重载=操作符 |
四、set 数据插入与删除
函数原型:insert、erase、clear | 解释 | |
---|---|---|
1 | insert(elem) | 在容器中插入元素 |
2 | erase(elem) | 删除容器中值为elem的元素 |
3 | erase(pos) | 删除pos迭代器所指的元素,返回下一个元素的迭代器 |
4 | erase(beg, end) | 删除区间[beg,end)的所有元素,返回下一个元素的迭代器 |
5 | clear() | 清除所有元素 |
示例1:
#include <iostream>
#include <string>
#include <set> //必须包含该头文件
using namespace std;
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(30);
s1.insert(50);
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(20); //set插入重复元素失败,size不会发生变化
cout << "插入后的元素:" ;
printSet(s1); //set插入后的数据会自动排序
}
int main()
{
test01();
system("pause");
return 0;
}
//result
插入后的元素:10 20 30 40 50
注意:
set
容器插入后的数据会自动排序set
容器插入重复元素会失败,元素数量不会发生改变
示例2:
#include <iostream>
#include <string>
#include <set> //必须包含该头文件
using namespace std;
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);
s1.insert(50);
cout << "插入后的元素:" ;
printSet(s1);
//删除
s1.erase(s1.begin()); //删除第一个元素
cout << "erase(s1.begin())删除后的元素:" ;
printSet(s1);
s1.erase(30); //删除值为30的元素
cout << "erase(30)删除后的元素:" ;
printSet(s1);
//清空
//s1.erase(s1.begin(), s1.end()); //同clear()
s1.clear();
cout << "清空后的元素:" ;
printSet(s1);
}
int main()
{
test01();
system("pause");
return 0;
}
//result
插入后的元素:10 20 30 40 50
erase(s1.begin())删除后的元素:20 30 40 50
erase(30)删除后的元素:20 40 50
清空后的元素:
五、set 查找与统计
函数原型:find、count | 解释 | |
---|---|---|
1 | find(key) | 查找key是否存在, 若存在则返回该元素的迭代器;若不存在则返回set.end() |
2 | count(key) | 统计key的元素个数(对于set,结果为0或者1) |
注意:因为set
中元素不重复,所以count(key)
只能返回0或者1
示例:
#include <iostream>
#include <string>
#include <set> //必须包含该头文件
using namespace std;
void test01()
{
set<int> s1;
//插入
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
s1.insert(50);
//查找
set<int>::iterator it = s1.find(40);
if (it != s1.end())
{
cout << "找到元素: " << *it << endl;
}
else
{
cout << "未找到元素: " << endl;
}
//统计
int num = s1.count(50);
cout << "元素50数量 = " << num << endl;
num = s1.count(60);
cout << "元素60数量 = " << num << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//result
找到元素: 40
元素50数量 = 1
元素60数量 = 0
六、set 元素数量与交换
函数原型:empty、size、swap | 解释 | |
---|---|---|
1 | empty() | 判断容器是否为空 |
2 | size() | 返回容器中元素的数量 |
3 | swap(set<T> & st) | 交换两个容器中的元素 |
注意:swap()
交换的两个容器中包含元素的类型必须相同。
七、set 迭代器
函数原型:begin、end | 解释 | |
---|---|---|
1 | begin() | 返回一个迭代器,指向set容器的首元素 |
2 | end() | 返回一个迭代器,指向set容器的末尾元素的下一个位置 |
3 | rbegin() | 返回一个反向迭代器,指向set容器的最后一个元素 |
4 | rend() | 返回一个反向迭代器,指向set容器的首元素的上一个位置 |
5 | cbegin() | 返回一个const迭代器,指向set容器的首元素 |
6 | cend() | 返回一个const迭代器,指向set容器的末尾元素的下一个位置 |
7 | crbegin() | 返回一个const反向迭代器,指向set容器的末尾元素 |
8 | crend() | 返回一个const反向迭代器,指向set容器的首的上一个位置 |
注意:const iterator
是一个指向const
值的迭代器。迭代器本身可以修改,但是它不能被用来修改它所指向的值。
八、set 容器排序
- set容器默认排序规则为从小到大
- 利用仿函数,可以改变排序规则
- 对于自定义数据类型,set必须指定排序规则才可以插入数据
示例1: 内置数据类型排序
#include <iostream>
#include <string>
#include <set> //必须包含该头文件
using namespace std;
class Compare
{
public:
bool operator()(int v1, int v2) //仿函数,重载operator()运算符
{
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, Compare> s2;
s2.insert(10);
s2.insert(40);
s2.insert(20);
s2.insert(30);
s2.insert(50);
for (set<int, Compare>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//result
10 20 30 40 50
50 40 30 20 10
示例2: 自定义数据类型排序
#include <iostream>
#include <string>
#include <set> //必须包含该头文件
#include <string>
using namespace std;
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) //仿函数,重载operator()运算符
{
//按照年龄进行排序 从大到小
return p1.m_Age > p2.m_Age;
}
};
void test01()
{
set<Person, comparePerson> s;
Person p1("张三", 44);
Person p2("李四", 32);
Person p3("王五", 66);
Person p4("赵六", 19);
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();
system("pause");
return 0;
}
//result
姓名: 王五 年龄: 66
姓名: 张三 年龄: 44
姓名: 李四 年龄: 32
姓名: 赵六 年龄: 19
九、set 与 multiset 区别
set
不允许容器中有重复的元素multiset
允许容器中有重复的元素set
插入数据的同时会返回插入结果,表示插入是否成功multiset
不会检测数据,因此可以插入重复数据
示例:
#include <iostream>
#include <string>
#include <set> //必须包含该头文件
using namespace std;
void test01()
{
multiset<int> s1;
//插入
s1.insert(10);
s1.insert(40);
s1.insert(40);
s1.insert(40);
s1.insert(40);
//查找
multiset<int>::iterator it = s1.find(40);
if (it != s1.end())
{
cout << "找到元素: " << *it << endl;
}
else
{
cout << "未找到元素: " << endl;
}
//统计
int num = s1.count(40);
cout << "40 元素数量 = " << num << endl;
num = s1.count(60);
cout << "60 元素数量 = " << num << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//result
找到元素: 40
40 元素数量 = 4
60 元素数量 = 0