文章目录
STL常用容器——set容器的使用
1、set容器简介
set 容器是存储排序键的关联式容器,使用 set 容器存储的各个键值对,要求键 key 和值 value 必须相等,如下:
{<'a', 'a'>, <'b', 'b'>, <'c', 'c'>}
set容器底层结构是用二叉树实现的。
set容器会自动根据键的大小对存储的键值对进行排序,根据 key 排序,也就等价为根据 value 排序。
set 容器中每个键都是唯一的,可以插入或删除但不能修改,因为修改很有可能破坏 set 容器中元素的有序性。
最正确的修改 set 容器中元素值的做法是:先删除该元素,然后再添加一个修改后的元素。
2、set容器的构造和赋值
set st; //默认构造函数,在构造容器时还可以对容器进行初始化
//默认构造,
set<int> s1;
//在构造容器时还可以对容器进行初始化
set<int> s2{4,6,3,7,2};
set(const set &st) //拷贝构造函数,将已有 set 容器中存储的所有元素全部拷贝到新 set 容器中
set<int> s3(s2);
//等同于赋值
//set<int> s3 = s2
移动构造函数,创建新 set 容器的同时利用临时的 set 容器为其初始化
在初始化 s5容器时,内部调用的是 set 类模板中的移动构造函数,而不是拷贝构造函数。
//返回值是一个临时 set 容器
set<int> retSet() {
set<int> s4{5,4,8,2,3,9,1};
return s4;
}
set<int> s5(retSet());
截取已有 set 容器中的部分元素,来初始化新的 set 容器
set<int> s6(++s5.begin(), --s5.end());
代码实例
#include<iostream>
using namespace std;
#include<set>
void printSet(set<int>& s) {
for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
set<int> retSet() {
set<int> s4{5,4,8,2,3,9,1};
return s4;
}
void test01() {
//默认构造,
set<int> s1;
//在构造容器时还可以对容器进行初始化
set<int> s2{4,6,3,7,2};
printSet(s2);
//拷贝构造
set<int> s3(s2);
//等同于赋值
//set<int> s3 = s2
printSet(s3);
set<int> s5(retSet());
printSet(s5);
set<int> s6(++s5.begin(), --s5.end());
printSet(s6);
}
int main() {
test01();
system("pause");
return 0;
}
3、set容器的大小和交换
成员方法 | 功能 |
---|---|
size() | 返回当前 set 容器中元素的个数。 |
empty() | 判断容器是否为空,为空则返回 true;否则 false。 |
swap() | 交换 2 个 set 容器中存储的所有元素,这 2 个 set 容器的类型必须相同。 |
#include<iostream>
using namespace std;
#include<set>
void printSet(set<int>& s) {
for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test02() {
set<int> s1{10,30,50,40,10};
if (!s1.empty()) {
cout << "s1不为空" << endl;
printSet(s1);
cout << "s1容器的元素数目" << s1.size() << endl;
}
else {
cout << "s1为空" << endl;
}
set<int> s2{1,3,5,4,1};
cout << "交换前" << endl;
printSet(s1);
printSet(s2);
s1.swap(s2);
cout << "交换后" << endl;
printSet(s1);
printSet(s2);
}
int main() {
test02();
system("pause");
return 0;
}
4、set容器的添加与删除
成员方法 | 功能 |
---|---|
insert(val) | 向 set 容器中添加元素。 |
clear() | 清空 set 容器中所有的元素,即令 set 容器的 size() 为 0。 |
erase(pos) | 删除pos位置上的元素,返回下一个元素的迭代器。 |
erase(beg,end) | 删除区间[ beg,end ) 的元素,返回下一个元素的迭代器。 |
erase(elem) | 删除容器中元素为elem的元素。 |
#include<iostream>
using namespace std;
#include<set>
void printSet(set<int>& st) {
for (set<int>::iterator it = st.begin(); it != st.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test01() {
set<int> s1;
//添加
s1.insert(60);
s1.insert(20);
s1.insert(20);
s1.insert(50);
s1.insert(30);
printSet(s1);
//清除指定位置的元素
s1.erase(s1.begin());
printSet(s1);
//清除指定元素
s1.erase(50);
printSet(s1);
//清除区间内的元素
set<int> s2{2,5,7,5,6,3};
s2.erase(++s2.begin(), --s2.end());
printSet(s2);
//清除所有
s2.clear();
printSet(s2);
}
int main() {
test01();
system("pause");
return 0;
}
5、set容器的查找与统计
成员方法 | 功能 |
---|---|
find(val) | 在 set 容器中查找值为 val 的元素,如果成功找到,则返回指向该元素的双向迭代器;反之,则返回和 end() 方法一样的迭代器。另外,如果 set 容器用 const 限定,则该方法返回的是 const 类型的双向迭代器。 |
count(val) | 在当前 set 容器中,查找值为 val 的元素的个数,并返回。注意,由于 set 容器中各元素的值是唯一的,因此该函数的返回值最大为 1。 |
#include<iostream>
using namespace std;
#include<set>
void test02() {
set<int> s1;
s1.insert(60);
s1.insert(20);
s1.insert(20);
s1.insert(50);
s1.insert(30);
set<int>::iterator it = s1.find(30);
if (it != s1.end()) {
cout << "找到元素" << endl;
}
else {
cout << "未找到元素" << endl;
}
int num = s1.count(30);
cout << "30在容器s1个个数为" << num << endl;
}
int main() {
test02();
system("pause");
return 0;
}
6、pair类模板
pair(对组)将一对值组合成一个值,利用对组可以返回两个数据。
这一对值可以具有不同的数据类型(T1和T2)。
两个值可以分别用pair的两个公有函数first和second访问。
创建pair对组的方式
-
pair(); 默认构造函数
-
pair<type,type> p( value1,value2); 直接使用 2 个元素初始化成 pair 对象
-
pair(const pair<U,V>& pr) 拷贝(复制)构造函数
-
pair<type,type> p = make_pair(value1,value2);
#include<iostream>
using namespace std;
#include<string>
void test01() {
pair<string, int> p1;
pair<string, int> p2("张三", 18);
cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
pair<string, int> p3 = make_pair("李四", 36);
cout << "姓名:" << p3.first << " 年龄:" << p3.second << endl;
pair<string, int> p4(p3);
cout << "姓名:" << p4.first << " 年龄:" << p4.second << endl;
}
int main() {
test01();
system("pause");
return 0;
}
7、set和multiset的区别
- set不可以插入重复的元素,multiset可以插入重复的元素
- set插入元素的同时会返回插入结界,表示插入是否成功
- multiset不会检测数据,因此可以插入重复数据
#include<iostream>
using namespace std;
#include<set>
void test01() {
set<int> s1;
pair<set<int>::iterator, bool> res = s1.insert(10);
if (res.second) {
cout << "第一次插入成功" << endl;
}
else {
cout << "第一次插入失败" << endl;
}
res = s1.insert(10);
if (res.second) {
cout << "第二次插入成功" << endl;
}
else {
cout << "第二次插入失败" << endl;
}
multiset<int> s2;
s2.insert(10);
s2.insert(10);
cout << "容器s2的元素数目" << s2.size() << endl;
int num1 = s1.count(10);
int num2 = s2.count(10);
cout << "容器s1中元素10的数目为" << num1 << endl;
cout << "容器s2中元素10的数目为" << num2 << endl;
}
int main() {
test01();
system("pause");
return 0;
}
8、set容器的排序
set容器默认是升序排序,利用仿函数,可以改变排序规则
#include<iostream>
using namespace std;
#include<set>
#include<string>
class MyCompare {
public:
bool operator()(int v1, int v2) const {
return v1 > v2;
}
};
void test01()
{
//创建容器时指定排序规则
set<int, MyCompare> s1{ 10 ,20 ,50,30};
for (set<int, MyCompare>::iterator it = s1.begin(); it != s1.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
//自定义数据类型,需要自己指定排序规则
class MyCompare2 {
public:
bool operator()( Person p1, Person p2)const
{
return p1.m_Age > p2.m_Age;
}
};
void test02() {
set<Person, MyCompare2> s1();
Person p1("张三", 32);
Person p2("关羽", 42);
Person p3("刘备", 50);
Person p4("赵云", 22);
s1.insert(p1);
s1.insert(p2);
s1.insert(p3);
s1.insert(p4);
for (set<Person, MyCompare2>::iterator it = s1.begin(); it != s1.end(); it++) {
cout << "姓名: " << (*it).m_Name << " 年龄:" << (*it).m_Age << endl;
}
}
int main() {
test01();
test02();
system("pause");
return 0;
}