文章目录
一. set基本概念
- 所有的元素在插入的时候会被自动排序
set
和multiset
都是关联式容器,底层结构都是用二叉树实现
二. set的构造和赋值
set<T> s
默认构造函数
set(const set& s);
拷贝构造函数
set& operator=(const set& s);
// 重载等号操作符
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/
#include <iostream>
using namespace std;
#include<set>
void print_set(const set<int> &s)
{
for (set<int>::const_iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test_01(void)
{
set<int> s;
s.insert(10);
s.insert(40);
s.insert(30);
s.insert(20);
s.insert(20);
s.insert(30);
// set容器的特点:
// 1. 插入的元素会自动排序
// 2. 插入的元素不允许有重复元素,就算是有重复元素,不会报错,但是不会插入
print_set(s); // 10 20 30 40
// 拷贝构造
set<int> s2(s);
print_set(s2);
// 赋值操作符
set<int> s3 = s2;
print_set(s3);
}
int main()
{
test_01();
system("pause");
return 0;
}
三. set的大小和交换
原型:
size()
返回容器中元素的个数
empty();
判断容器是否为空
swap(s);
交换两个集合容器
注意:
set容器不能更改大小,因为不允许有重复元素,所以更改大小之后填充的值没办法确定
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/
#include <iostream>
using namespace std;
#include <set>
void print_set(const set<int> &s)
{
for (set<int>::const_iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test_01(void)
{
set<int> s1;
s1.insert(10);
s1.insert(20);
s1.insert(40);
s1.insert(30);
print_set(s1);
if (s1.empty())
{
cout << "s1为空!" << endl;
}
else
{
cout << "s1不为空,s1的大小: " << s1.size() << endl;
}
set<int> s2;
s2.insert(100);
s2.insert(300);
s2.insert(200);
// 交换操作
cout << "交换前: " << endl;
cout << "s1 = ";
print_set(s1);
cout << "s2 = ";
print_set(s2);
s1.swap(s2);
// 交换后
cout << "交换后: " << endl;
cout << "s1 = ";
print_set(s1);
cout << "s2 = ";
print_set(s2);
}
int main()
{
test_01();
system("pause");
return 0;
}
四. set的插入和删除
insert(elem);
在容器中插入元素
clear()
清除所有的元素
erase(pos);
删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(begin,end);
删除区间[begin,end]的所有元素,返回下一个元素的迭代器
erase(elem);
删除容器中值为elem的元素
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/
#include <iostream>
using namespace std;
#include<set>
void print_set(const set<int> &s)
{
for (set<int>::const_iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test_01(void)
{
set<int> s1;
// 插入
s1.insert(30);
s1.insert(10);
s1.insert(20);
s1.insert(40);
print_set(s1);
// 删除第一个元素
s1.erase(s1.begin());
print_set(s1);
// 删除指定元素
s1.erase(20);
print_set(s1);
// 删除区间
s1.erase(s1.begin(), s1.end());
set<int> s2;
// 插入
s2.insert(30);
s2.insert(10);
s2.insert(20);
s2.insert(40);
s2.clear(); // 清空集合
if (s2.empty())
{
cout << "集合为空!" << endl;
}
else
{
cout << "集合不为空,集合大小为: " << s2.size() << endl;
}
}
int main()
{
test_01();
system("pause");
return 0;
}
五. set的查找和统计
find(key);
查找key
是否存在,若存在,返回该键的元素的迭代器,若不存在,则返回set.end();
count(key);
统计key的元素的个数,对于set而言,无非就是0和1.因为不允许有重复元素
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/
#include <iostream>
using namespace std;
#include <set>
void print_set(const set<int> &s)
{
for (set<int>::const_iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test_01(void)
{
set<int> s;
s.insert(10);
s.insert(20);
s.insert(40);
s.insert(30);
set<int>::iterator pos;
pos = s.find(10);
if (pos != s.end())
{
cout << "查找成功,查找到的元素是: " << *pos << endl;
}
else
{
cout << "查找失败" << endl;
}
int num = s.count(10); // 集合set的元素的个数只能返回的是0和1
// 因为set是没有重复元素的,所以个数要不就是存在只有一个,要不就是一个都没有
cout << "10的个数: " << num << endl;
}
int main()
{
test_01();
system("pause");
return 0;
}
六. set 和 multiset的区别
set
不可以插入重复数据,而multiset
可以set
插入数据的同时会返回插入结果,表示插入是否成功multiset
不会检测数据,因此可以插入重复数据
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/
#include <iostream>
using namespace std;
#include<set>
void test_01(void)
{
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<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()
{
test_01();
system("pause");
return 0;
}
结论:
如果要插入重复的数据,请选择
multiset
,如果不插入重复的数据,可以使用set
,set
插入的时候有一个对组的返回值,第二个值是bool类型,表示是否插入成功
七. pair对组创建
成对出现的数据,利用对组可以返回两个数据
创建方式:
pair<type,type> p (value1,value2);
pair<type,type> p = make_pair(value1,value2);
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/
#include <iostream>
using namespace std;
#include<set>
void test_01(void)
{
// 第一种方式
pair<string, int> p("Tom", 20);
cout << "姓名: " << p.first << " 年龄: " << p.second << endl;
// 第二种方式
pair<string, int> p2 = make_pair("Fioman", 30);
cout << "姓名: " << p2.first << " 年龄: " << p2.second << endl;
}
int main()
{
test_01();
system("pause");
return 0;
}
八. set容器的排序
set
容器的默认排序规则是从小到大,但是利用一些技术也是可以改变排序规则的- 利用仿函数,可以改变排序规则
- 在
set
创建的时候模板那里加入一个仿函数,使用这个仿函数作为规则进行排序.
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/
#include <iostream>
using namespace std;
#include<set>
class MyCompare
{
public:
bool operator()(int v1, int v2)const // 这里主要要是const函数
{
return v1 > v2;
}
};
void print_set(const set<int> &s)
{
for (set<int>::const_iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test_01(void)
{
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
print_set(s1); // 10 20 30 40
set<int,MyCompare> s2; // 如果要更改排序规则,这里创建的时候就要加上
s2.insert(10);
s2.insert(30);
s2.insert(20);
s2.insert(40);
// 降序打印,变成了40 30 20 10
for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test_01();
system("pause");
return 0;
}