【C++】蓝桥必备-STL<Part 6-set>

100 阅读3分钟

STL第六部分(set)

✨1、set【集合】

① 头文件

#include <set>

② 初始化

set<string> st1;

③ 区别

set不允许元素重复,如果有重复就会被忽略,但multiset允许。

④ 常用函数

1. size() 返回元素个数

// 返回元素个数
st1.size();

2. empty() 检查集合是否为空

// 检查集合是否为空
st1.empty();

3. clear() 清空集合

// 清空集合
st1.clear();

4. insert() 向集合插入元素

// 向集合插入元素  插入的元素类型必须与定义的集合类型相同
// 插入方式一 
st1.insert(15);
st1.insert(15);
st1.insert(5);
st1.insert(1);
st1.insert(20);
// 插入方式二:元素被插入到定义的位置 
auto itr = st2.insert(st2.begin(), 1);

itr = st2.insert(itr, 4);
itr = st2.insert(itr, 2);
itr = st2.insert(itr, 5);
itr = st2.insert(itr, 3); 
std::cout << "st1:";
for(auto i = st1.begin(); i!=st1.end(); i++){
    std::cout << *i << " ";
}  // 1 5 15 20
std::cout << endl;

std::cout << "st2:";
for(auto i = st2.begin(); i!=st2.end(); i++){
    std::cout << *i << " ";
}  // 1 2 3 4 5
std::cout << endl;

5. begin()和end() 角标为0的数和最后一个数

st1.insert(15);
st1.insert(5);
st1.insert(1);
st1.insert(20);
for(auto i = st1.begin(); i!=st1.end(); i++){  // 从角标为0的数遍历到最后一个数
    std::cout << *i << " ";  // 输出
}  //  1 5 15 20

6. find() 查找一个数

// 如果找到该值返回该值,如果没有找到,则返回返回指向集合末尾的迭代器,即st1.end()
std::cout << "st1.find(15):" << *st1.find(15) << endl;  // 15
std::cout << "st1.find(100):" << *st1.find(100) << endl; // 4

7. count() 返回某一个数的个数

// 自我感觉没必要,集合中如果有重复就会被忽略,只显示一个
st1.insert(15);
st1.insert(15);
st1.insert(5);
st1.insert(1);
st1.insert(20);
std::cout << "st1.count(15):" << st1.count(15) << endl; // 1

8. erase() 删除某个值

若集合中不存在这个元素,不进行任何操作。

// 删除某个数 
st1.erase(15); 
std::cout << "删除集合中的15后:" << *st1.find(15) << endl;  // 3
st1.erase(22);
std::cout << "删除集合中没有的22后:" << *st1.end() << endl;  // 3

⑤ 核心函数

1. lower_bound() 核心操作

//返回大于等于x的最小的数的迭代器 
std::cout << "大于等于5的最小的数:" << *st1.lower_bound(5) << endl; // 5

2. upper_bound()

//返回大于x的最小的数的迭代器 
std::cout << "大于5的最小的数:" << *st1.upper_bound(5) << endl; // 20

image.png

①至⑤运行截图

附全文代码

#include <iostream>
#include <set>

using namespace std;

int main(){
	
	set<int> st1, st2;
	
	// 返回元素个数
	std::cout << "st1.size():" << st1.size() << endl;
	std::cout << "st1.empty():" << st1.empty() << endl;
	
	// 插入方式一 
	st1.insert(15);
	st1.insert(15);
	st1.insert(5);
	st1.insert(1);
	st1.insert(20);
	// 插入方式二:元素被插入到定义的位置 
	auto itr = st2.insert(st2.begin(), 1);
 
    itr = st2.insert(itr, 4);
    itr = st2.insert(itr, 2);
    itr = st2.insert(itr, 5);
    itr = st2.insert(itr, 3); 
    std::cout << "st1:";
	for(auto i = st1.begin(); i!=st1.end(); i++){
		std::cout << *i << " ";
	}
	std::cout << endl;

	std::cout << "st2:";
	for(auto i = st2.begin(); i!=st2.end(); i++){
		std::cout << *i << " ";
	}
	std::cout << endl;
	
	// 查找某个数
	std::cout << "st1.find(15):" << *st1.find(15) << endl;  // 15
	std::cout << "st1.find(100):" << *st1.find(100) << endl; // 4
	
	
	std::cout << "st1.count(15):" << st1.count(15) << endl; // 1
	
	// 删除某个数 
	st1.erase(15); 
	std::cout << "删除集合中的15后:" << *st1.find(15) << endl;  // 3
	st1.erase(22);
	std::cout << "删除集合中没有的22后:" << *st1.end() << endl;  // 3
	
	
	//返回大于等于x的最小的数的迭代器 
	std::cout << "大于等于5的最小的数:" << *st1.lower_bound(5) << endl; // 5
	//返回大于x的最小的数的迭代器 
	std::cout << "大于5的最小的数:" << *st1.upper_bound(5) << endl; // 20
	
	return 0;
}