基本概念
set
简介:
所有元素都会在插入时自动被排序
本质:
set、multiset属于关联式容器,底层结构是用二叉树实现
set multiset区别
set 不允许容器中有重复的元素
multiset 允许容器中有重复的元素
构造和赋值
#include<bits/stdc++.h>
using namespace std;
#include<set>
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(20);
s1.insert(10);
s1.insert(40);
s1.insert(23);
//遍历容器
//set容器特点:所有元素插入时候自动被排序
//set容器不允许插入重复值
printSet(s1);
//拷贝构造
set<int>s2(s1);
printSet(s2);
set<int>s3;
s3 = s1;
printSet(s3);
}
int main()
{
test01();
return 0;
}
运行结果
10 20 23 40
10 20 23 40
10 20 23 40
pair对组创建
#include<bits/stdc++.h>
using namespace std;
#include<set>
void test01()
{
//第一种方式
pair<string, int>p("Tom", 20);
cout << "姓名:" << p.first << " 年龄:" << p.second << endl;
//第二种方式
pair<string, int>p2 = make_pair("Jerry", 30);
cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
}
int main()
{
test01();
return 0;
}
运行结果
姓名:Tom 年龄:20
姓名:Jerry 年龄:30
大小和交换
#include<bits/stdc++.h>
using namespace std;
#include<set>
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;
//插入数据
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(30);
printset(s1);
if (s1.empty())
{
cout << "s1为空" << endl;
}
else
{
cout << "s1不为空" << endl;
cout << "s1的大小为:" << s1.size() << endl;
}
}
void test02()
{
set<int>s1, s2;
//插入数据
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(30);
//插入数据
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);
}
int main()
{
test02();
return 0;
}
运行结果
交换前:
10 20 30 40
100 200 300 400
交换后:
100 200 300 400
10 20 30 40
set和multiset区别
#include<bits/stdc++.h>
using namespace std;
#include<set>
void printSet(set<int>& s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s;
pair <set<int>::iterator, bool>ret = s.insert(10);
if (ret.second)
{
cout << "第一次插入成功" << endl;
}
else
{
cout << "第一次插入失败" << endl;
}
ret = s.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;
}
int main()
{
test01();
return 0;
}
运行结果
第一次插入成功
第二次插入失败
10 10
排序
实质
重载()
#include<bits/stdc++.h>
using namespace std;
#include<set>
class MyCompare
{
public:
bool operator()(int v1,int v2)const
{
return v1 > v2;
}
};
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(20);
s1.insert(40);
s1.insert(30);
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//指定排序规则从大到小
set<int, MyCompare>s2;
s2.insert(10);
s2.insert(20);
s2.insert(40);
s2.insert(30);
for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
return 0;
}
10 20 30 40
40 30 20 10
自定义数据类型排序
#include<bits/stdc++.h>
using namespace std;
#include<set>
class Person
{
public:
Person(string name, int age)
{
this->name = name;
this->age = age;
}
string name;
int age;
};
class MyCompare
{
public:
/*bool operator()(Person& p1, Person& p2)const
{
return p1.age>p2.age;
}*/
bool operator()(Person p1, Person p2)const
{
//按照年龄进行排序 降序
return p1.age > p2.age;
}
};
void test01()
{
set<Person, MyCompare>s;
//创建Person对象
Person p1("刘备", 24);
Person p2("关羽", 28);
Person p3("张飞", 25);
Person p4("赵云", 21);
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->name << " 年龄:" << it->age << endl;
}
}
int main()
{
test01();
return 0;
}
运行结果
姓名:关羽 年龄:28
姓名:张飞 年龄:25
姓名:刘备 年龄:24
姓名:赵云 年龄:21