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 << '	' << 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 << '	' << 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

原文地址:https://www.cnblogs.com/charlee44/p/12499417.html