目录
一、基本概念
所有元素都会在插入时自动被排序
本质:
- set/multiset属于关联式容器,底层结构是用二叉树实现
区别:
- set不允许容器中有重复的元素
- multiset允许容器中有重复的元素
二、set构造和赋值
构造
- set<T> st; //默认构造函数
- set(const set& st); //拷贝构造函数
赋值
- set& operator=(const set &st); //重载等号操作符
#include <iostream>
#include <set>
using namespace std;
void printset(const set<int>& st)
{
for(set<int>::const_iterator it = st.begin(); it != st.end(); it++)
{
cout << *it <<" ";
}
cout << endl;
}
void test01()
{
set<int> s1;
//插入数据 只有insert方式
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(30);
s1.insert(20);
printset(s1);
}
int main()
{
test01();
}
三、set大小和交换
统计set容器大小以及交换set容器
函数原型
- size(); //返回容器中元素的数目
- empty(); //判断容器是否为空
- swap(st); //交换两个集合容器
void test01()
{
set<int> s1;
//插入数据 只有insert方式
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(30);
s1.insert(20);
printset(s1);
//判断容器是否为空
if (s1.empty())
{
cout << "s1为空";
}
else
{
cout << "s1不为空" << endl;
cout << "s1的大小为: " << s1.size();
}
}
void test02()
{
set<int> s1;
//插入数据 只有insert方式
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(30);
set<int> s2;
s2.insert(100);
s2.insert(400);
s2.insert(200);
s2.insert(300);
cout << "交换前: " << endl;
printset(s1);
printset(s2);
cout << "交换后: " << endl;
s1.swap(s2);
printset(s1);
printset(s2);
}
四、set插入和删除
函数原型
- insert(); //在容器中插入元素
- clear(); //清除所有元素
- erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器
- erase(beg,end); //删除区间【beg,end】的所有元素,返回下一个元素的迭代器
- erase(elem); //删除容器值中值为elem的元素
void test01()
{
set<int> s1;
//插入数据 只有insert方式
s1.insert(30);
s1.insert(40);
s1.insert(20);
s1.insert(10);
printset(s1);
//删除
s1.erase(s1.begin());
printset(s1);
//删除重载版本
s1.erase(30);
printset(s1);
//清空
//s1.erase(s1.begin(),s1.end());
s1.clear();
printset(s1);
}
五、set查找和统计
对set容器进行查找数据以及统计数据
函数原型
- find(key); //查找key是否存在,若存在返回该元素的迭代器;若不存在,返回set.end();
- count(key); //统计key元素的个数
set<int> s1;
//插入数据 只有insert方式
s1.insert(30);
s1.insert(40);
s1.insert(20);
s1.insert(10);
printset(s1);
//查找
set<int>::iterator pos = s1.find(30);
if (pos != s1.end())
{
cout << "找到了元素: " << *pos << endl;
}
else
{
cout << "未找到元素" << endl;
}
//统计
int num = s1.count(30);
//对于set容器 统计结果 要么为0,要么为1
cout << "num:30 个数 " << num << endl;
六、set和multiset区别
区别
- set不可以插入重复数据,而multiset可以
- set插入数据的同时会返回插入结果,表示插入是否成功
- multiset不会检测数据,因此可以插入重复数据
set<int> s;
pair<set<int>::iterator ,bool> ret = s.insert(10);
if (ret.second)
{
cout << "第1次插入成功" << endl;
}
else
{
cout << "第1次插入失败" << endl;
}
ret = s.insert(10);
if (ret.second)
{
cout << "第2次插入成功" << endl;
}
else
{
cout << "第2次插入失败" << 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;
七、pair对组创建
成对出现的数据,利用对组可以返回两个数据
两种创建方式
- pair<type, type> p(value1, value2);
- pair<type, type> p =make_pair(value1, value2);
//第一种方式
pair<string, int> p("TOM", 99);
cout << "姓名: " << p.first << "年龄: " << p.second << endl;
//第二种方式
pair<string, int> p2 = make_pair("Jerry", 88);
cout << "姓名: " << p2.first << "年龄: " << p2.second << endl;
八、set容器排序
set容器默认排序为从小到大,掌握如何改变排序规则
主要技术点:
- 利用仿函数,可以改变排序规则
1、内置数据类型
#include <iostream>
#include <set>
using namespace std;
class MyCompare
{
public:
bool operator()(int v1, int v2)const
{
return v1 > v2;
}
};
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(40);
s1.insert(30);
s1.insert(88);
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//指定排序规则为从大到小
set<int,MyCompare>s2;
s2.insert(10);
s2.insert(40);
s2.insert(30);
s2.insert(88);
for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
}
2、自定义数据类型
#include <iostream>
#include <set>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
m_name = name;
m_age = age;
}
string m_name;
int m_age;
};
class ComparePerson
{
public:
bool operator()(const Person& p1, const Person& p2)const
{
//按照年龄 降序
return p1.m_age > p2.m_age;
}
};
void test01()
{
//创建Person对象
Person p1("刘备", 99);
Person p2("关羽", 78);
Person p3("赵云", 88);
Person p4("张飞", 68);
//自定义数据类型 需先指定排序规则
set<Person, ComparePerson> s;
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
for (set<Person>::iterator it = s.begin(); it != s.end(); it++)
{
cout << "姓名:" <<(*it).m_name << " 年龄:"<<(*it).m_age<<endl;
}
cout << endl;
}
int main()
{
test01();
}