蓝桥杯-常用STL(二)

常用STL

🎈1.集合

🔎集合是数学中的一个基本概念,通俗的理解,集合就是一些不重复的数据组成的。比如{1,2,3}就是一个有1,2,3三个元素的集合。在C++中,我们常用的集合是set

🎈2.set的基础使用

🔭2.1引入库

C++set的实现是在一个<set>头文件中,在代码开头引入这个头文件,并且同样加上一句using namespace std;

🔭2.2插入元素

C++中用insert()函数向集合中插入一个新的元素。注意如果集合中已经存在了某个元素,再次插入不会产生任何效果,集合中是不会出现重复元素的。

cpp 复制代码
#include <set>
#include <iostream>
using namespace std;
int main()
{
	set<string> country;//{}
	country.insert("China");//{"China"}
	country.insert("America");//{"China","America"}
	country.insert("France");//{"China","America","France"}
	country.insert("China");//{"China","America","France"}
	return 0;
}

🔭2.3删除元素

C++中用erase()函数删除集合中的一个元素,如果集合不存在这个元素,不进行任何操作。

cpp 复制代码
#include <set>
#include <iostream>
using namespace std;
int main()
{
	set<string> country;//{}
	country.insert("China");//{"China"}
	country.insert("America");//{"China","America"}
	country.insert("France");//{"China","America","France"}
	country.insert("China");//{"China","America","France"}
	country.erase("China");//{"America","France"}
	country.erase("England");//{"America","France"}
}

🔭2.4判断元素是否存在

C++中如果你想知道某个元素是否在集合中出现,你可以直接用count()函数。如果集合中存在我们要查找的元素,返回1,否则返回0

cpp 复制代码
#include <set>
#include <iostream>
using namespace std;
int main()
{
	set<string> country;//{}
	country.insert("China");//{"China"}
	country.insert("America");//{"China","America"}
	country.insert("France");//{"China","America","France"}
	if (country.count("China"))
		cout << "China belong to country." << endl;
	return 0;
}

🔭2.5遍历元素

C++通过迭代器可以访问集合中的每个元素,迭代器就好像一根手指指向set中发某个元素。通过操作这个手指,我们可以改变它要指向的元素。通过*(解引用操作符)操作可以获取迭代器指向的元素。通过++操作让迭代器指向下一个元素,同理,--操作让迭代器指向上一个元素。

迭代器的写法比较固定,set<T>::iterator it就定义了一个指向set<T>这种集合的迭代器it,T是任意的数据类型。其中::iterator是固定的写法。begin函数返回容器中起始元素的迭代器,end函数返回容器的尾后迭代器。

cpp 复制代码
#include <set>
#include <iostream>
using namespace std;
int main()
{
	set<string> country;//{}
	country.insert("China");//{"China"}
	country.insert("America");//{"China","America"}
	country.insert("France");//{"China","America","France"}
	for (set<string>::iterator it = country.begin(); it != country.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	return 0;
}

❗**注意:**在C++中遍历set是从小到大遍历的,也就是说set会帮我们排好序。

🔭2.6清空

C++中调用clear()函数就可以清空set,同时会清空set占用的内存。

🎈3.set与结构体

set经常会配合结构体来使用,用set来储存结构体和vector有些区别。正如我们前面所说的那样,set是需要经过排序的,系统自带的数据类型有默认的比较大小的规则,而我们自定义的结构体,系统是不可能知道这个结构体比较大小的方式的。所以,我们需要用一种方式来告诉系统怎么比较这个结构体的大小。其中一种方法叫运算符重载,我们需要重新定义小于符号。

cpp 复制代码
struct Node
{
	int x, y;
	bool operator<(const Node& rhs)const 
	{
		if (x == rhs.x)
			return y < rhs.y;
		else
			return x < rhs.x;
	}
};

operator<表示我们要重载运算符<,可以看成一个函数名。rhs是"right hand side"的简称,有右操作数的意思,这里我们定义为一个const引用。因为该运算符重载定义在结构体内部,左操作数就当前调用operator<的对象。

特别要注意,不要漏掉最后的const,const函数表示不能对其他数据成员进行修改操作,并且const对象不能调用非const成员函数,只允许调用const成员函数。

上面重载规定了排序方式为:优先依照x从小到大排序,如果x相同,那么再按照y从小到大排序。经过了<运算符重载的结构体,我们就可以比较两个Node对象的大小了,因此可以直接储存在set中了。

✅通过上面的学习,我们学会用set来储存一个二维坐标系上的点的集合。接下来,我们来给这个结构体添加<的运算符重载,让编译器知道该怎么对这个结构体比较大小。

cpp 复制代码
#include <iostream>
#include <set>
using namespace std;
struct Point
{
	int x;
	int y;
	bool operator<(const Point& rhs)const
	{
		if (x == rhs.x)
			return y < rhs.y;
		else
			return x < rhs.x;
	}
};
int main()
{
	int n;
	set<Point>v;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		Point temp;
		cin >> temp.x >> temp.y;
		v.insert(temp);
	}
	cout << "排序后:" << endl;
	for (set<Point>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << it->x << " " << (*it).y << endl;
	}
	return 0;
}
相关推荐
PassLink_2 小时前
AlgorithmVisualizer项目改进与部署-网页算法可视化
算法·编程·开源项目·本地部署·算法可视化·源码改进
测试老哥2 小时前
Postman参数化详解
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·postman
GalaxyPokemon3 小时前
LeetCode - 2. 两数相加
java·前端·javascript·算法·leetcode·职场和发展
编程绿豆侠3 小时前
力扣HOT100之堆:347. 前 K 个高频元素
算法·leetcode·哈希算法
GalaxyPokemon5 小时前
归并排序:分治思想的高效排序
数据结构·算法·排序算法
ThreeYear_s5 小时前
基于FPGA的PID算法学习———实现PI比例控制算法
学习·算法·fpga开发
Coding小公仔7 小时前
LeetCode 240 搜索二维矩阵 II
算法·leetcode·矩阵
C++chaofan7 小时前
74. 搜索二维矩阵
java·算法·leetcode·矩阵
Studying 开龙wu8 小时前
机器学习监督学习实战五:六种算法对声呐回波信号进行分类
学习·算法·机器学习
Mi Manchi268 小时前
力扣热题100之二叉树的层序遍历
python·算法·leetcode