C++中自定义结构体或类作为关联容器的键

文章目录

1. 概述

STL中像set和map这样的容器是通过红黑树来实现的,插入到容器中的对象是顺序存放的,采用这样的方式是非常便于查找的,查找效率能够达到O(log n)。所以如果有查找数据的需求,可以采用set或者map。

但是我们自定义的结构体或者类,无法对其比较大小,在放入到容器中的时候,就无法正常编译通过,这是set/map容器的规范决定的。要将自定义的结构体或者类存入到set/map容器,就需要定义一个排序的规则,使其可以比较大小。最简单的办法就是在结构体或者类中加入一个重载小于号的成员函数,这样在存数据进入set/map中时,就可以根据其规则排序。

2. 实例

在这里就写了一个简单的例子,将自定义的一个二维点存入set/map,并查找其中存入的数据:

#include <iostream>
#include <map>
#include <set>
#include <string>

using namespace std;

const double EPSILON = 0.000001;

// 2D Point
struct Vector2d
{
public:
	Vector2d()
	{
	}

	Vector2d(double dx, double dy)
	{
		x = dx;
		y = dy;
	}

	// 矢量赋值
	void set(double dx, double dy)
	{
		x = dx;
		y = dy;
	}

	// 矢量相加
	Vector2d operator + (const Vector2d& v) const
	{
		return Vector2d(x + v.x, y + v.y);
	}

	// 矢量相减
	Vector2d operator - (const Vector2d& v) const
	{
		return Vector2d(x - v.x, y - v.y);
	}

	//矢量数乘
	Vector2d Scalar(double c) const
	{
		return Vector2d(c*x, c*y);
	}

	// 矢量点积
	double Dot(const Vector2d& v) const
	{
		return x * v.x + y * v.y;
	}

	//向量的模
	double Mod() const
	{
		return sqrt(x * x + y * y);
	}

	bool Equel(const Vector2d& v) const
	{
		if (abs(x - v.x) < EPSILON && abs(y - v.y) < EPSILON)
		{
			return true;
		}
		return false;
	}

	bool operator == (const Vector2d& v) const
	{
		if (abs(x - v.x) < EPSILON && abs(y - v.y) < EPSILON)
		{
			return true;
		}
		return false;
	}

	bool operator < (const Vector2d& v) const
	{		
		if (abs(x - v.x) < EPSILON)
		{
			return y < v.y ? true : false;
		}
		return x<v.x ? true : false;
	}

	double x, y;
};


int main()
{	
	{
		set<Vector2d> pointSet;	
		pointSet.insert(Vector2d(0, 11));
		pointSet.insert(Vector2d(27, 63));
		pointSet.insert(Vector2d(27, 15));
		pointSet.insert(Vector2d(0, 0));
		pointSet.insert(Vector2d(67, 84));
		pointSet.insert(Vector2d(52, 63));

		for (const auto &it : pointSet)
		{
			cout << it.x << '\t' << it.y << endl;
		}

		auto iter = pointSet.find(Vector2d(27, 63));
		if (iter == pointSet.end())
		{
			cout << "未找到点" << endl;
		}
		else
		{
			cout << "可以找到点" << endl;
		}
	}	

	{
		map<Vector2d, string> pointSet;
		pointSet.insert(make_pair(Vector2d(52, 63), "插入时的第1个点"));
		pointSet.insert(make_pair(Vector2d(27, 63), "插入时的第2个点"));
		pointSet.insert(make_pair(Vector2d(0, 11), "插入时的第3个点"));		
		pointSet.insert(make_pair(Vector2d(67, 84), "插入时的第4个点"));
		pointSet.insert(make_pair(Vector2d(27, 15), "插入时的第5个点"));
		pointSet.insert(make_pair(Vector2d(0, 0), "插入时的第6个点"));
	
		for (const auto &it : pointSet)
		{
			cout << it.first.x << ',' << it.first.y << '\t' << it.second << endl;
		}

		auto iter = pointSet.find(Vector2d(27, 63));
		if (iter == pointSet.end())
		{
			cout << "未找到点" << endl;
		}
		else
		{
			cout << "可以找到点" << endl;
		}
	}
}

其中的关键就是在点的结构体中重载了<符号的比较函数,规定首先比较y的大小,其次在比较x的大小:

bool operator < (const Vector2d& v) const
{		
    if (abs(x - v.x) < EPSILON)
    {
        return y < v.y ? true : false;
    }
    return x<v.x ? true : false;
}

最终的运行结果如下:

imglink1

C++,std::map是一个有序的关联容器,它的元素按照的大小进行排序。如果想要自定义排序规则,可以使用自定义比较函数或者lambda表达式来实现。 引用的代码示例展示了如何利用自定义比较函数来对std::map进行排序。首先,将std::map的值对拷贝到一个std::vector,然后使用std::sort函数对std::vector进行排序。在自定义比较函数cmp_val,我们定义了按照对应的值进行排序的规则。最后,通过遍历排序后的std::vector来输出排序后的结果。 引用的代码示例展示了使用lambda表达式来实现相同的功能。在std::sort函数的第三个参数,我们使用了一个lambda表达式来定义排序规则,即按照对应的值进行排序。 以下是一个类似的示例代码,用于自定义排序std::map: ```cpp #include <iostream> #include <string> #include <map> #include <algorithm> #include <vector> using namespace std; typedef pair<string, int> PAIR; struct cmp { bool operator()(const PAIR& left, const PAIR& right) const { return left.second < right.second; } }; int main() { map<string, int> ma; ma["Alice"] = 86; ma["Bob"] = 78; ma["Zip"] = 92; ma["Stdevn"] = 88; vector<PAIR> vec(ma.begin(), ma.end()); sort(vec.begin(), vec.end(), cmp()); for (vector<PAIR>::iterator ite = vec.begin(); ite != vec.end(); ++ite) { cout << ite->first << " " << ite->second << endl; } return 0; } ``` 在这个示例,我们定义了一个结构体cmp作为自定义比较函数,并在main函数创建一个map对象ma,然后将值对拷贝到一个vector,并使用自定义比较函数cmp对vector进行排序。最后,通过遍历排序后的vector来输出结果。 请注意,这只是一个示例代码,你可以根据实际需求进行修改和调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++的map的自定义排序](https://blog.csdn.net/sinat_31608641/article/details/128122868)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C++ map自定义排序](https://blog.csdn.net/frighting_ing/article/details/122027936)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

charlee44

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

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

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

打赏作者

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

抵扣说明:

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

余额充值