目录
1.Set基本概念
简介:
- 所有元素都会在插入时自动被排序
本质:
- set/multiset属于关联式容器 底层结构是用二叉树实现
set和multiset区别:
- set不允许容器中有重复的元素
- multiset允许容器总有重复的元素
2.set构造和赋值
功能描述:
创建set容器以及赋值
/*
构造
set<int> 默认构造函数
set<const set &st> 拷贝构造函数
赋值
set &operator=(const set &st) 重载等号操作符
*/
void printSet(set<int>& s) {
for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
cout << *it << endl;
}
}
void test01() {
set<int> s1;
//插入数据 只有insert方式
s1.insert(40);
s1.insert(30);
s1.insert(20);
s1.insert(10);
//遍历容器
//set容器特点:所有元素插入时候自动被排序
//set容器不允许插入重复值
printSet(s1);///输出:10 20 30 40
//拷贝构造
set<int> s2(s1);
//赋值
set<int> s3 = s2;
}
int main() {
test01();
system("pause");
return 0;
}
3.set大小和交换
功能描述:
统计set容器大小及交换set容器
/*
size() 返回容器中元素的数目
empty() 判断容器是否为空
swap(st) 交换两个集合容器
*/
void printSet(set<int>& s) {
for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
cout << *it << endl;
}
}
void test01() {
set<int> s1;
//插入数据
s1.insert(40);
s1.insert(50);
s1.insert(20);
s1.insert(10);
//打印容器
printSet(s1);
//判断容器是否为空
if (s1.empty()) {
cout << "s1为空" << endl;
}else {
cout << "s1不为空" << endl;
cout << "s1的大小为:" << s1.size() << endl;
}
//交换
set<int> s2;
s2.insert(12);
s2.insert(52);
s2.insert(16);
s2.insert(24);
s2.swap(s1);
printSet(s1);
}
int main() {
test01();
system("pause");
return 0;
}
4.set插入和删除
功能描述:
set容器进行插入数据和删除数据
/*
insert() 在容器中插入元素
clear() 清除所有元素
erase(pos) 删除pos迭代器所指的元素 返回下一个元素的迭代器
erase(beg,end) 删除区间(beg,end)的所有元素 返回下一个元素的迭代器
erase(elem) 删除容器中值为elem的元素
迭代器不是随机迭代器
*/
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(40);
s1.insert(50);
s1.insert(20);
s1.insert(10);
//遍历容器
printSet(s1);
//删除迭代器所指的内容
s1.erase(s1.begin());
printSet(s1);
//删除重载 删除值为elem的
s1.erase(50);
printSet(s1);
//删除 另类清空
s1.erase(s1.begin(), s1.end());
//清空
s1.clear();
}
int main() {
test01();
system("pause");
return 0;
}
5.set查找和统计
功能描述:
对set容器进行查找数据以及统计数据
/*
find(key) 查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end()
count(key) 统计key的元素个数
*/
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(40);
s1.insert(50);
s1.insert(20);
s1.insert(10);
//查找
//迭代器接收数据
set<int>::iterator pos = s1.find(30);
if (pos != s1.end()) {
cout << "找到元素: " << *pos << endl;
}else {
cout << "未找到元素: " << endl;
}
//统计
int num = s1.count(50);
//对于set而言,统计结果要么是0,要么是1 因为set容器不能存放重复数据
cout << num << endl;
}
int main() {
test01();
system("pause");
return 0;
}
6.set和multiset区别
区别:
- set不可插入重复数据,而multiset可以
- set插入数据的同时会返回插入结果,表示插入是否成功
- multiset不会检测数据,因此可以插入重复数据
7.set容器的排序
set容器默认排序规则为从小到大,可以改变默认排序规则
7.1 set存放内置的数据类型如何改变排序方式
#include "set和multiset.h"
#include<iostream>
#include <set>
using namespace std;
//set容器排序
class MyCompare {
public:
bool operator()( int v1, int v2) const {
return v1 > v2;
}
};
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(40);
s1.insert(50);
s1.insert(20);
s1.insert(10);
//内置函数排序从小到大
printSet(s1);
//指定排序规则为从大到小
set<int,MyCompare> s2;
//插入数据
s2.insert(40);
s2.insert(50);
s2.insert(20);
s2.insert(10);
for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) {
cout << *it << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
7.2 set存放自定义数据类型如何改变排序方式
class Person {
public:
string m_Name;
int m_Age;
Person(string name,int age) {
this->m_Name = name;
this->m_Age = age;
}
};
class comparePerson {
public:
bool operator()(const Person& p1, const Person& p2) {
//按照年龄 降序
return p1.m_Age > p2.m_Age;
}
};
void test01() {
set<Person> s1;
//创建Person对象
Person p1("刘备",35);
Person p2("曹操",45);
Person p3("孙权",30);
Person p4("诸葛亮",35);
Person p5("司马懿",40);
Person p6("鲁肃",35);
s1.insert(p1);
s1.insert(p2);
s1.insert(p3);
s1.insert(p4);
s1.insert(p5);
s1.insert(p6);
for (set<Person,comparePerson>::iterator it = s1.begin(); it != s1.end(); it++) {
cout <<"姓名:" << it->m_Name <<"年龄: " << it->m_Age << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}