蓝桥杯-常用STL(二)

🎈1.集合

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

🎈2.set的基础使用

🔭2.1引入库

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

🔭2.2插入元素

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

#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()函数删除集合中的一个元素,如果集合不存在这个元素,不进行任何操作。

#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

#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函数返回容器的尾后迭代器。

#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是需要经过排序的,系统自带的数据类型有默认的比较大小的规则,而我们自定义的结构体,系统是不可能知道这个结构体比较大小的方式的。所以,我们需要用一种方式来告诉系统怎么比较这个结构体的大小。其中一种方法叫运算符重载,我们需要重新定义小于符号。

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来储存一个二维坐标系上的点的集合。接下来,我们来给这个结构体添加<的运算符重载,让编译器知道该怎么对这个结构体比较大小。

#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;
}

在这里插入图片描述

  • 25
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一口⁵个团子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值