文章目录
12.8 set/multiset 容器
12.8.1 set基本概念
简介:所有元素都会插入时自动被排序
本质:set/multiset 属于关联式容器,底层结构是用二叉树实现。
set/multiset区别
- set 不允许容器中有重复的元素
- multiset允许容器中有重复的元素
12.8.2 set构造和赋值
功能描述:创建set容器以及赋值
构造:
- set st; //默认构造函数
- set(const set &st); //拷贝构造函数
赋值:
- set& operator=(const set &st); //重载等号操作符
示例代码:
#include<iostream>
using namespace std;
#include<set>
//set容器构造和赋值
void printSet(const 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(3);
s1.insert(2);
s1.insert(1);
s1.insert(4);
s1.insert(4);
//遍历容器
//set容器特点,所有元素插入时候自动被排序
//set容器不允许插入重复值
printSet(s1);
//拷贝构造
set<int>s2(s1);
printSet(s2);
//赋值操作
set<int>s3;
s3 = s2;
printSet(s3);
}
int main()
{
test01();
system("pause");
return 0;
}
运行结果:
总结:
- set容器插入数据是用 - insert
- set容器插入数据的数据会自动排序
12.8.3 set大小和交换
功能描述:
- 统计set容器大小以及交换set容器
函数原型:
- size(); //返回容器中元素的数目
- empty(); //判断容器是否为空
- swap(); //交换练个集合容器
示例代码:
#include<iostream>
using namespace std;
#include<set>
//set容器大小和交换
void printSet2(set<int>& s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//大小
void test21()
{
set<int>s1;
//插入数据
s1.insert(1);
s1.insert(3);
s1.insert(2);
s1.insert(4);
//打印数据
printSet2(s1);
//判断是否为空
if (s1.empty())
{
cout << "s1为空" << endl;
}
else
{
cout << "s1不为空" << endl;
}
set<int>s2;
//插入数据
s2.insert(30);
s2.insert(10);
s2.insert(40);
s2.insert(20);
cout << "交换前:" << endl;
printSet2(s1);
printSet2(s2);
s1.swap(s2);//交换数据
cout << "交换后:" << endl;
printSet2(s1);
printSet2(s2);
}
int main()
{
test21();
system("pause");
return 0;
}
运行结果:
总结:
- 获取容器大小 ---- size
- 判断容器是否为空—empty
- 交换容器数据 ------ swap
12.8.4 set容器插入和删除
功能描述:
- set容器进程插入和删除数据
函数原型:
- insert(elem); //在容器中插入元素
- clear(); //清除所有元素
- erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器
- erase(beg, end); //删除区间[ beg ,end ]的所有元素,返回下一个元素的迭代器
- erase(elem); //删除容器中值为elem的元素
示例代码:
#include<iostream>
using namespace std;
#include<set>
//set容器 插入和删除操作
void printSet3(set<int>& s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test31()
{
set<int>s1;
//插入数据
s1.insert(3);
s1.insert(4);
s1.insert(2);
s1.insert(1);
printSet3(s1);
//删除
s1.erase(s1.begin());
printSet3(s1);
//删除重载版本
s1.erase(4);
printSet3(s1);
//清空
//s1.erase(s1.begin(), s1.end());
s1.clear();
printSet3(s1);
}
int main()
{
test31();
system("pause");
return 0;
}
运行结果:
总结:
- 插入 — insert
- 删除 — erase
- 清空 — clear
12.8.5 set查找和统计
功能描述:
- 对set容器进行查找数据以及统计数据
函数原型:
- find(key); //查找key是否存在,返回该键的元素的迭代器;若不存在,返回set.end();
- count(key); //统计key的元素个数
示例代码:
#include<iostream>
using namespace std;
#include<set>
void printSet4(set<int>& s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//set容器 查找和统计
void test41()
{
set<int>s1;
//插入数据
s1.insert(30);
s1.insert(20);
s1.insert(40);
s1.insert(10);
//查找
set<int>::iterator pos = s1.find(2);
if (pos!=s1.end())
{
cout << "找到元素:" << *pos << endl;
}
else
{
cout << "没有找到元素:" << endl;
}
}
void test42()
{
set<int>s2;
//插入数据
s2.insert(30);
s2.insert(20);
s2.insert(40);
s2.insert(10);
s2.insert(30);
s2.insert(30);
printSet4(s2);
//统计30个数
int num = s2.count(30);
//对于set而言,统计结果要么是1要么是0
cout << " num = " << num << endl;
}
int main()
{
//test41();
test42();
system("pause");
return 0;
}
运行结果:
总结:
- 查找 — find (返回的是迭代器)
- 统计 — count (对于set ,结果为0或1)
12.8.6 set 和 multiset区别
学习目标:
- 掌握set 和 multiset 的区别
区别:
- set不可以插入重复数据,而multiset可以
- set 插入数据的同时会返回插入结果,表示插入是否成功
- multiset不会检测数据,因此可以插入重复数据
示例代码:
#include<iostream>
using namespace std;
#include<set>
//set容器和multiset容器的区别
void test51()
{
set<int>s;
pair<set<int>::iterator, bool> ret = s.insert(10);
if (ret.second)
{
cout << "第一次插入成功" << endl;
}
else
{
cout << "第一次插入失败" << endl;
}
//第二次插入10
ret = s.insert(10);
if (ret.second)
{
cout << "第二次插入成功" << endl;
}
else
{
cout << "第二次插入失败" << endl;
}
//multiset允许插入重复值
multiset<int>ms;
ms.insert(20);
ms.insert(20);
for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test51();
system("pause");
return 0;
}
运行结果:
总结:
- 如果不允许插入重复数据可以利用set
- 如果想要插入重复数据利用multiset
12.8.7 pair对组创建
功能描述:
- 成对出现的数据,利用对组可以返回两个数据
两种创建方式:
- pair<type,type> p (value1, value2);
- pair<type,type> p =make_pair (value1, value2);
示例代码:
#include<iostream>
using namespace std;
#include<string>
//pair对组的创建
void test61()
{
//第一种方式
pair<string, int>p("Tom",20);
cout << "姓名:" << p.first << " 年龄:" << p.second<< endl;
//第二种方式
pair<string, int>p2 = make_pair("Jack",19);
cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
}
int main()
{
test61();
system("pause");
return 0;
}
运行结果:
总结:
两种方式都可以创建对组,记住一种即可。
12.8.8 set容器排序
学习目标:
- set容器默认排序规则为从小到大,掌握如何改变排序规则
主要技术点:
- 利用仿函数,可以改变排序规则
示例1: set存放内置数据类型
#include<iostream>
using namespace std;
#include<set>
//set容器排序
//用仿函数指定排序规则
class MyCompare
{
public:
bool operator()(int v1, int v2) const
{
return v1 > v2; //当v1>v2时,返回true
}
};
void printSet7(set<int>& s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test71()
{
set<int>s1;
s1.insert(20);
s1.insert(10);
s1.insert(30);
s1.insert(40);
s1.insert(50);
printSet7(s1);
//指定23为从小到大排序
set<int, MyCompare>s2;
s2.insert(20);
s2.insert(10);
s2.insert(30);
s2.insert(40);
s2.insert(50);
for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test71();
system("pause");
return 0;
}
运行结果:
总结: 利用仿函数可以指定set容器的排序规则
示例二: set存放自定义数据类型
#include<iostream>
using namespace std;
#include<set>
#include<string>
//set容器排序,存放自定义数据类型
class Person
{
public:
string m_name;
int m_age;
Person(string name, int age)
{
m_name = name;
m_age = age;
}
};
//创建仿函数
class comparePerson
{
public:
bool operator()(const Person& p1,const Person& p2) const
{
//按照年龄降序
return p1.m_age > p2.m_age;
}
};
void test81()
{
//自定义数据类型,都需要指定排序规则
set<Person, comparePerson>s;
//创建Person对象
Person p1("李白", 26);
Person p2("杜普", 25);
Person p3("白居易", 36);
Person p4("屈原", 42);
Person p5("李清照", 28);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
s.insert(p5);
for (set<Person, comparePerson>::iterator it = s.begin(); it != s.end(); it++)
{
cout << "姓名:" << (*it).m_name << " 年龄:" << (*it).m_age << endl;
}
}
int main()
{
test81();
system("pause");
return 0;
}
运行结果:
总结:
- 自定义数据类型,都需要指定排序规则
如果对你有帮助的话,请不要忘了给我一点点点…支持 ( ^ o ^)/~