Java 转 C++ 系列:STL容器之set
·
文章参考:黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难
文章目录
一、set/multiset基本概念
set/multiset 属于关联式容器,底层结构是用红黑树实现,所有元素都会在插入时自动被排序。
set和multiset区别:
- set不允许容器中有重复的元素
- multiset允许容器中有重复的元素
使用 set 要求添加头文件 #include<set>
二、set使用方法
2.1 set构造和赋值
// 构造
set<T> st; , 默认构造函数
set(const set &st); , 拷贝构造函数
// 赋值
set& operator=(const set &st); , 重载等号操作符
示例代码:
set<int> s1;
//s1 = { 10, 30, 20, 40 };
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40); // 10 20 30 40
//拷贝构造
set<int>s2(s1);
//赋值
set<int>s3;
s3 = s2; //10 20 30 40
注:set容器插入数据的数据会自动排序
set<int> s1 = { ... }; //初始化 列表构造函数
set<int> s1; s1 = { ... }; //赋值 重载 operator=
2.2 set大小和交换
size(); , 返回容器中元素的数目
empty(); , 判断容器是否为空
swap(st); , 交换两个集合容器
示例代码:
set<int> s1 = { 10, 30, 20, 40 }; // 10 20 30 40
if (s1.empty()) {
cout << "s1为空" << endl;
}
else {
cout << "s1不为空" << endl;
cout << "s1的大小为: " << s1.size() << endl; // 4
}
set<int> s2 = { 1, 3, 2, 4 }; // 1 2 3 4
s2.swap(s1);
// s1: 1 2 3 4
// s2: 10 20 30 40
2.3 set插入和删除
insert(elem); , 在容器中插入元素
clear(); , 清除所有元素
erase(pos); , 删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(beg, end); , 删除区间[beg,end)的所有元素,返回下一个元素的迭代器
erase(elem); , 删除容器中值为elem的元素
示例代码:
set<int> s1;
//插入
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40); // 10 20 30 40
//删除
//s1.erase(s1.begin()); // 20 30 40
s1.erase(++s1.begin()); // 10 30 40
s1.erase(30); // 10 40
//清空
s1.erase(s1.begin(), s1.end());
s1.clear();
2.4 set查找和统计
find(key); , 查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key); , 统计key的元素个数
示例代码:
set<int> s1 = { 10, 30, 20, 40 };// 10 20 30 40
//查找
set<int>::iterator pos = s1.find(30);
if (pos != s1.end()) {
cout << "找到了元素 : " << *pos << endl;
}
else {
cout << "未找到元素" << endl;
}
//统计
int num = s1.count(30);
cout << "num = " << num << endl; // 1
2.5 迭代器遍历
以 set<int>& s 为例:
void printSet(const set<int>& s) {
for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
// C++11简化版本
void printSet(const set<int>& s) {
for (const auto& val : s) { // const auto&:既避免拷贝,又保证不修改元素,最规范
cout << val << " ";
}
cout << endl;
}
三、扩展
3.1 set和multiset区别
- set不可以插入重复数据,而multiset可以
- set插入数据的同时会返回插入结果,表示插入是否成功
- multiset不会检测数据,因此可以插入重复数据
示例代码:
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 << " ";
}
cout << endl;
打印结果:
3.2 pair对组创建
成对出现的数据,利用对组可以返回两个数据,两种创建方式:
pair<type, type> p ( value1, value2 );
pair<type, type> p = make_pair( value1, value2 );
示例代码:
pair<string, int> p(string("Tom"), 20);
cout << "姓名: " << p.first << " 年龄: " << p.second << endl;
pair<string, int> p2 = make_pair("Jerry", 10);
cout << "姓名: " << p2.first << " 年龄: " << p2.second << endl;
3.1 set容器排序
案例一:set存放内置数据类型
#include<iostream>
#include<string>
#include<set>
using namespace std;
class MyCompare {
public:
// 重载()
bool operator()(int v1, int v2) const {
return v1 > v2;
}
};
int main() {
system("chcp 65001> nul");
//指定排序规则
set<int, MyCompare> s1;
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(30);
s1.insert(50);
// 50 40 30 20 10
for (set<int, MyCompare>::iterator it = s1.begin(); it != s1.end(); it++) {
cout << *it << " ";
}
cout << endl;
return 0;
}
注:利用仿函数可以指定set容器的排序规则
案例二:set存放自定义数据类型
#include<iostream>
#include<string>
#include<set>
using namespace std;
class Person {
public:
Person(string name, int age) : m_Name(name), m_Age(age) {}
string m_Name;
int m_Age;
};
class ComparePerson {
public:
bool operator()(const Person& p1, const Person& p2) const {
//按照年龄进行排序 降序
return p1.m_Age > p2.m_Age;
}
};
int main() {
system("chcp 65001> nul");
set<Person, comparePerson> s;
Person p1("刘备", 23);
Person p2("关羽", 27);
Person p3("张飞", 25);
Person p4("赵云", 21);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
for (set<Person, ComparePerson>::iterator it = s.begin(); it != s.end(); it++) {
cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age << endl;
}
system("pause");
return 0;
}
注:对于自定义数据类型,set必须指定排序规则才可以插入数据
更多推荐


所有评论(0)