set/multiset
7 set/multiset容器
7.1 set基本概念
简介:
- 所有元素都会在插入时自动被排序
本质:
- set/multiset属于关联式容器,底层结构是用二叉树实现
set/multiset区别:
- set不允许容器中有重复元素
- multiset允许容器中有重复元素
7.2 set构造和赋值
功能描述:
- 创建set容器以及赋值
构造:
- set st; // 默认构造函数
- set(const set &st); // 拷贝构造函数
赋值:
- set & operator=(const set & st); // 重载等号操作符
代码示例:
#include<iostream>
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 test_01()
{
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
printSet(s1); // 10 20 30 40
// 拷贝构造
set<int> s2(s1);
printSet382(s2); // 10 20 30 40
// 赋值
set<int> s3;
s3 = s2;
printSet382(s3); // 10 20 30 40
}
int main()
{
test_01();
system("pause");
return 0;
}
7.3 set大小和交换(size、empty、swap)
功能描述:
- 统计set容器大小以及交换set容器
函数原型:
- size(); // 返回元素中容器数量
- empty(); // 判断容器是否为空
- swap(); // 交换两个集合容器
代码示例:
#include<iostream>
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 test_01()
{
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
if (s1.empty())
{
cout << "s1为空" << endl;
}
else
{
cout << "s1不为空" << endl;
cout << "s1的大小为:" <<s1.size() << endl; // s1的大小为:4
}
}
void test_02()
{
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
set<int> s2;
s2.insert(100);
s2.insert(300);
s2.insert(200);
cout << "交换前" << endl;
printSet(s1); // 10 20 30 40
printSet(s2); // 100 200 300
cout << endl;
s1.swap(s2);
cout << "交换后" << endl;
printSet(s1); // 100 200 300
printSet(s2); // 10 20 30 40
cout << endl;
}
int main()
{
test_01();
test_02();
system("pause");
return 0;
}
7.4 set插入和删除(insert、erase、clear)
功能描述:
- set容器进行插入数据和删除数据
- set容器中插入的数据会自动排序
函数原型:
- insert(elem); // 在容器中插入元素
- clear(); // 清除所有元素
- erase(); // 删除pos迭代器所指元素,返回下一个元素的迭代器
- erase(); // 删除区间[beg, end)的所有元素,返回下一个元素的迭代器
- erase(elem); // 删除容器中值为elem的元素
代码示例:
#include<iostream>
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 test_01()
{
set<int> s1;
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(40);
printSet(s1); // 10 20 30 40
// 删除
s1.erase(s1.begin());
printSet(s1); // 20 30 40
s1.erase(30);
printSet(s1); // 20 40
// 清空
s1.clear();
printSet(s1);
}
int main()
{
test_01();
system("pause");
return 0;
}
7.5 set查找和统计(find, count)
功能描述:
- 对set容器进行查找数据以及统计数据
函数原型:
- find(key); // 查找key是否存在,若存在,返回该键的元素的迭代器,若不存在,返回set.end()
- count(); // 统计key的元素个数
代码示例:
#include<iostream>
using namespace std;
#include<set>
void test_01()
{
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(50);
s1.insert(40);
//查找
set<int>::iterator pos = s1.find(50);
if (pos != s1.end())
{
cout << "找到了元素:" << *pos << endl; // 找到了元素:50
}
else
{
cout << "未找到元素" << endl;
}
// 统计
int num = s1.count(30);
cout << "num = " << num << endl; // num = 1
}
int main()
{
test_01();
system("pause");
return 0;
}
7.6 set与multiset区别
区别:
- set不可以插入重复数据,而multiset可以
- set插入数据的同时会返回插入结果,表示插入是否成功
- multiset不会检测数据,因此可以插入重复数据
代码示例:
#include<iostream>
using namespace std;
#include<set>
void test_01()
{
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
multiset<int> ms;
ms.insert(10);
ms.insert(10);
for(multiset<int>::iterator it=ms.begin();it!=ms.end();it++)
{
cout << *it << endl; // 10 10
}
}
int main()
{
test_01();
system("pause");
return 0;
}
7.7 pair对组创建(pair, make_pair)
功能描述:
- 成对出现的数据,利用对组可以返回两个数据
两种创建方式:
- pair<type, type> p (value1, value2);
- pair<type, type> p = make_pair(value1, value2);
代码示例:
#include<iostream>
using namespace std;
#include<set>
#include<string>
// 对组创建
void test_01()
{
pair<string, int> p(string("Tom"), 20);
cout << "姓名:" << p.first << "年龄" << p.second << endl; // 姓名:Tom年龄20
pair<string, int> p2 = make_pair("Jerry", 10);
cout << "姓名:" << p2.first << "年龄" << p2.second << endl; // 姓名:Jerry年龄10
}
int main()
{
test_01();
system("pause");
return 0;
}
7.8 set容器排序
主要技术点:
- set容器默认排序规则为从小到大,利用仿函数,可以改变排序规则
代码示例:
#include<iostream>
using namespace std;
#include<set>
// 利用仿函数可以指定set容器的排序规则
class MyCompare
{
public:
bool operator()(int v1, int v2) const
{
return v1 > v2;
}
};
void test_01()
{
set<int> s1;
s1.insert(10);
s1.insert(40);
s1.insert(30);
s1.insert(20);
s1.insert(50);
// 默认从小到大
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " "; // 10 20 30 40 50
}
cout << endl;
// 指定规则
set<int, MyCompare> s2;
s2.insert(10);
s2.insert(40);
s2.insert(30);
s2.insert(20);
s2.insert(50);
for (set<int>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " "; // 50 40 30 20 10
}
cout << endl;
}
int main()
{
test_01();
system("pause");
return 0;
}