所有元素都会在插入时自动被排序
本质:
set/multiset属于关联式容器,底层结构是用二叉树实现
set和multiset区别:
set不允许容器中有重复的元素
multiset允许容器中有重复的元素
构造:
set<T>st;
set(const set& st);
赋值:
set& operator=(cosnt set& st);
void print(set<int>&s)
{
for(set<int>::iterator it=s.begin();it!=s.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
int main()
{
set<int>s1;
//插入数据,只有insert方式
//set容器不允许插入重复值
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(40);
s1.insert(50);
//遍历容器
print(s1);
set<int>s2(s1);
print(s2);
}
插入数据,只有insert方式
set大小和交换
函数原型:
size();
//返回容器中元素的数目empty();
//判断容器是否为空swap(st);
//交换两个集合容
set不可以重新指定大小
set<int>s1;
//插入数据,只有insert方式
//set容器不允许插入重复值
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(40);
s1.insert(50);
//遍历容器
print(s1);
set<int>s2;
for(int i=0;i<10;i++)
{
s2.insert(i);
}
print(s2);
if(s2.empty())
{
cout<<"s2 is empty"<<endl;
}
else
{
cout<<"s2 is not empty"<<endl;
}
s2.swap(s1);
print(s1);
set容器插入和删除
函数原型:
insert(elem);
//在容器中插入元素。clear();
//清除所有元素erase(pos);
//删除pos迭代器所指的元素,返回下一个元素的迭代器。erase(beg, end);
//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。erase(elem);
//删除容器中值为elem的元素。
set<int>s1;
//插入数据,只有insert方式
//set容器不允许插入重复值
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(40);
s1.insert(50);
print(s1);
s1.erase(30);
s1.erase(s1.begin());
print(s1);
s1.clear();
print(s1);
set容器查找与统计
函数原型:
find(key);
//查找key是否存在,若存在,返回该键的元素的迭代器(位置);若不存在,返回set.end();count(key);
//统计key的元素个数
set<int>s1;
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(40);
s1.insert(50);
set<int>::iterator pos=s1.find(200);
if(pos != s1.end())
{
cout<<"find it"<<*pos<<endl;
}
else
{
cout<<"not find"<<endl;
}
int num=s1.count(300);
cout<<"nums of 30 is :"<<num<<endl;
set 和multiset的区别
区别:
- set不可以插入重复数据,而multiset可以
- set插入数据的同时会返回插入结果,表示插入是否成功
- multiset不会检测数据,因此可以插入重复数据
set<int>s1;
pair<set<int>::iterator,bool>ret=s1.insert(10);
if(ret.second)
{
cout<<"firt insert success"<<endl;
}
else
{
cout<<"fisrt insert fault"<<endl;
}
ret=s1.insert(10);
if(ret.second)
{
cout<<"firt insert success"<<endl;
}
else
{
cout<<"fisrt insert fault"<<endl;
}
multiset<int>ms;
ms.insert(100);
ms.insert(100);
for(multiset<int>::iterator it=ms.begin();it!=ms.end();it++)
{
cout<<*it<<endl;
}
pair对组创建
成对出现的数据,利用队组可以返回两个数据
使用的时候不需要包含头文件
两种创建方式:
pair<type,type>p(value1,value2);
pair<typr,type>p=make_pair(value1,value2);
int main()
{
pair<string ,int>p("tom",20);
cout<<"name: "<<p.first<<"age: "<<p.second<<endl;
pair<string ,int >p2=make_pair("lct",40);
cout<<"name: "<<p2.first<<"age: "<<p2.second<<endl;
}
set容器排序
set容器默认排序规则为从小到大
利用仿函数,可以改变排序规则
set存放内置统类型:
class mycompare
{
public:
bool operator()(int v1,int v2)
{
return v1>v2;
}
};
int main()
{
set<int>s1;
for(int i=0;i<10;i++)
{
s1.insert(i);
}
print(s1);
//指定排序规则为大到小
set<int,mycompare>s2;
for(int i=0;i<10;i++)
{
s2.insert(i);
}
for(set<int,mycompare>::iterator it=s2.begin();it!=s2.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
注意:加上仿函数之后,对应的迭代器也会发生改变,因此需要重新写一个迭代器来遍历容器
自定义数据类型:
class person
{
person(string name,int age)
{
this->m_name=name;
this->m_age=age;
}
public:
string m_name;
int m_age;
};
class compareperson
{
public:
bool operator()(const person&p1,const person &p2)
{
return p1.m_age>p2.m_age;
}
};
int main()
{
//自定义数据类型,都要指定排序规则
set<person,compareperson>s;
person p1("刘备",35);
person p2("曹操",45);
person p3("孙权",40);
person p4("赵云",25);
person p5("张飞",35);
person p6("关羽",35);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
s.insert(p5);
s.insert(p6);
for(set<person,compareperson>::iterator it=s.begin();it!=s.end();it++)
{
cout<<"name: "<<(*it).m_name<<" age"<<(*it).m_age<<" ";
}
}