简介:所有元素在插入时自动被排序
本质:set/multiset属于关联式容器,底层结构是用二叉树实现
set和multiset区别:
set不允许容器中有重复的元素
multiset允许容器中有重复的元素
set容器的构造函数和赋值
构造:
set st; 默认构造函数
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<<" ";
}
cout<<endl;
}
void test01(){
set<int>s1;
//插入数据 只有insert方式
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(30);
s1.insert(20);
//遍历set容器,特点:所有元素插入时自动被排序,set不允许插入重复值
printSet(s1);
//拷贝构造
set<int>s2(s1);
printSet(s1);
//重载=操作
set<int>s3;
s3 = s1 ;
printSet(s1);
}
set的大小和交换
函数原型:
size(); //返回容器中元素的数目
empty(); //判断容器是否为空
swap(); //交换两个集合容器
示例:
void test01(){
set<int>s1;
s1.insert(30);
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(30);
printSet(s1);
//set容器的大小
cout<<"s1容器的大小"<<s1.size()<<endl;
//判断容器是否为空
if(s1.empty()){
cout<<"s1容器为空"<<endl;
}
else
cout<<"s1容器为空"<<endl;
//容器的交换
set<int>s2;
s2.insert(300);
s2.insert(100);
s2.insert(400);
s2.insert(200);
s2.insert(300);
s2.swap(s1);
cout<<"---------交换后——————" <<endl;
printSet(s1);
printSet(s2);
}
set容器的插入和删除
函数原型:
insert(elem); //在容器中插入元素
clear(); //清楚所有元素
erase(pos); //删除pos迭代器所指元素,返回下一个元素位置的迭代器
erase(beg,end); //删除区间(beg,end)的所有元素,返回下一个元素的迭代器
erase(elem); //删除容器中为elem的元素
示例:
s2.clear();
cout<<"s2容器清空后的大小为: "<<s2.size()<<endl;
s1.erase(300);
printSet(s1);
s1.erase(++s1.begin());
printSet(s1);
s1.erase(s1.begin(),s1.end());
printSet(s1);
set容器的查找和统计
函数原型:
find(key); //查找key是否存在,返回该键的元素的迭代器,若不存在,返回set.end();
cout(key); //统计key的元素个数
示例:
//查找
set<int>::iterator pos = s1.find(200);
if(pos!=s1.end()){
cout<<"找到元素: "<<*pos<<endl;
}
else{
cout<<"未找到元素:"<<endl;
}
//统计,对于set而言,统计的结果要么是0,要么是1
int num = s1.count(100);
cout<<"num = "<<num<<endl;
set和multiset的区别
1、set不可以插入重复数据,而multiset可以
2、set插入数据的同时会返回插入结果,表示插入是否成功
3、multiset不会检测数据,因此可以插入重复数据
示例:
//set的multiset的区别
printSet(set<int>&s){
for(set<int>::const_iterator it=s.begin();it!=s.end();it++){
cout<<*it<<" ";
}
cout<<endl;
}
void test01(){
set<int>s1;
pair<set<int>::iterator,bool> ret = s1.insert(10);
if(ret.second){
cout<<"第一次插入成功"<<endl;
}
else{
cout<<"第一次插入失败"<<endl;
}
ret = s1.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;
}
pair对组创建 :成对出现的数据,利用对组可以返回两个数据
两种创建方式:
1、pair<type,type> p(value1,value2);
2、pair<type,type> p=make_pair(value1,value2);
示例:
//pair对组
void test01(){
//第一种方式:pair创建
pair<string,int>p1("Tom",10);
cout<<" 姓名:"<<p1.first<<" 年龄: "<<p1.second<<endl;
// 第二种方式:
pair<string,int>p2 = make_pair("Jerry",30);
cout<<"姓名: "<<p2.first<<"年龄: "<<p2.second<<endl;
}
输出样式:
set容器的排序
set容器默认是从小到大的排序,如何改变排序规则
只要应用的技术:仿函数,可以改变排序规则
示例:
1、set存放内置数据类型
void printSet(set<int>&s){
for(set<int>::iterator it = s.begin();it!=s.end();it++){
cout<<*it<<" ";
}
cout<<endl;
}
class MyCompare{
public:
// 重载()
bool operator()(int v1,int v2){
return v1>v2;
}
};
//set容器的排序
void test01(){
set<int>s1;
s1.insert(20);
s1.insert(10);
s1.insert(40);
s1.insert(30);
s1.insert(50);
printSet(s1);
//按从大到小排序
set<int,MyCompare>s2;
s2.insert(10);
s2.insert(40);
s2.insert(20);
s2.insert(50);
s2.insert(30);
//这里注意迭代器的定义
for(set<int,MyCompare>::iterator it = s2.begin();it!=s2.end();it++){
cout<<*it<<" ";
}
cout<<endl;
}
2、set中自定义自定义类型的排序
#include<iostream>
using namespace std;
#include<set>
#include<string>
class Person{
public:
string m_Name;
int m_Age;
Person(string name,int age){
this->m_Age = age;
this->m_Name = name;
}
};
//set容器的自定义类型排序,一般都需要自定义排序规则,不然插不进去
class comparePerson{
public:
bool operator()(const Person&p1,const Person&p2){
return p1.m_Age<p2.m_Age;
}
};
void test01(){
set<Person,comparePerson>s1;
Person p1("刘备",34);
Person p2("张飞",28);
Person p3("关羽",30);
Person p4("赵云",20);
s1.insert(p1);
s1.insert(p2);
s1.insert(p3);
s1.insert(p4);
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;
}
输出样式: