一丶Set容器介绍
- set元素再插入时都会自动排序
- set/multiset属于关联式容器,底层结构是用二叉树实现
set与multiset的区别:
- set容器中不允许有重复的元素
- multiset容器中允许有重复的元素
二丶Set容器的构造函数和赋值函数
函数原型
set<type> setName; //默认的构造函数
set(const set& se); //拷贝构造函数
set& operator=(const set& se); //重载操作符=
案例
#include<iostream>
using namespace std;
#include<set>
void printSet(const set<int>& s){
for (set<int>::iterator ptr = s.begin(); ptr != s.end();ptr++)
{
cout << *ptr << " ";
}
cout << endl;
}
void test01(){
set<int> s1;
s1.insert(10);
s1.insert(1);
s1.insert(30);
s1.insert(5);
cout << "s1为:\t";
printSet(s1);
set<int>s2 = s1;
cout << "s2为:\t";
printSet(s2);
set<int>s3(s2);
cout << "s3为:\t";
printSet(s3);
}
void main(){
test01();
system("pause");
return;
}
运行结果为:
三丶Set容器的大小和交换
函数原型
size(); //返回容器中的元素数目
empty(); //判断容器是否为空
swap(st); //交换两个集合容器
案例
#include<iostream>
using namespace std;
#include<set>
void printSet(const set<int>& s){
for (set<int>::iterator ptr = s.begin(); ptr != s.end();ptr++)
{
cout << *ptr << " ";
}
cout << endl;
}
void test01(){
set<int> s1;
s1.insert(10);
s1.insert(1);
s1.insert(30);
s1.insert(5);
cout << "s1为:";
printSet(s1);
cout << "s1中的元素个数为:" << s1.size() << endl;
if (s1.empty())
{
cout << "为空" << endl;
}
else
{
cout << "不为空" << endl;
}
set<int> s2;
s2.insert(101);
s2.insert(11);
s2.insert(301);
s2.insert(51);
s2.insert(5);
cout << "s2为:";
printSet(s2);
s1.swap(s2);
cout << "交换之后为:" << endl;
cout << "s1为:";
printSet(s1);
cout << "s2为:";
printSet(s2);
}
void main(){
test01();
system("pause");
return;
}
运行结果为:
四丶Set容器的插入和删除
函数原型
insert(elem); //插入elem元素
clear(); //清空set容器的元素
erase(beg,end); //删除区间[beg,end)的元素
erase(pos); //删除迭代器pos位置的元素,返回下一个元素的位置
erase(elem); //删除容器中所有跟elem值匹配的成员
案例
#include<iostream>
using namespace std;
#include<set>
void printSet(const set<int>& s){
for (set<int>::iterator ptr = s.begin(); ptr != s.end();ptr++)
{
cout << *ptr << " ";
}
cout << endl;
}
void test01(){
set<int> s1;
s1.insert(10);
s1.insert(1);
s1.insert(30);
s1.insert(5);
s1.insert(101);
s1.insert(11);
s1.insert(302);
s1.insert(53);
cout << "s1为:";
printSet(s1);
s1.erase(s1.begin());
cout << "************" << endl;
cout << "s1为:";
printSet(s1);
s1.erase(11);
cout << "************" << endl;
cout << "s1为:";
printSet(s1);
s1.erase(s1.begin(),(++(++s1.begin())));
cout << "************" << endl;
cout << "s1为:";
printSet(s1);
s1.clear();
cout << "************" << endl;
cout << "s1为:";
printSet(s1);
}
void main(){
test01();
system("pause");
return;
}
运行结果为:
五丶Set容器查找和统计
函数原型
find(elem); //查找elem是否存在,存在则返回该元素的迭代器,如果不存在则返回最后一个位置。
count(elem); //统计elem元素的个数
案例
#include<iostream>
using namespace std;
#include<set>
void printSet(const set<int>& s){
for (set<int>::iterator ptr = s.begin(); ptr != s.end();ptr++)
{
cout << *ptr << " ";
}
cout << endl;
}
void test01(){
set<int> s1;
s1.insert(10);
s1.insert(1);
s1.insert(30);
s1.insert(5);
s1.insert(10);
s1.insert(10);
cout << "s1为:";
printSet(s1);
cout << "**********************" << endl;
cout << "s1中10的个数为:\t" << s1.count(10) << endl;
cout << "s1中100的个数为:\t" << s1.count(100) << endl;
cout << "**********************" << endl;
set<int>::iterator ptr = s1.find(10);
if (ptr != s1.end())
{
cout << "已查找到元素:" << *ptr << endl;
}
else
{
cout << "未找到元素" << endl;
}
ptr = s1.find(105);
if (ptr != s1.end())
{
cout << "已查找到元素:" << *ptr << endl;
}
else
{
cout << "未找到元素" << endl;
}
}
void main(){
test01();
system("pause");
return;
}
运行结果为:
六丶Set容器和Multiset容器
multiset容器的操作和set容器基本相似,但是multiset容器可以插入重复的值。
案例
#include<iostream>
using namespace std;
#include<set>
void test02(){
multiset<int>ms;
ms.insert(10);
ms.insert(20);
ms.insert(30);
ms.insert(40);
ms.insert(10);
ms.insert(10);
for (multiset<int>::iterator ptr = ms.begin(); ptr != ms.end(); ptr++)
{
cout << *ptr << " ";
}
cout << endl;
}
void main(){
test02();
system("pause");
return;
}
运行结果为: