c/c++语言开发共享C++中自定义结构体或类作为关联容器的键

通过一个例子说明了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; }

最终的运行结果如下:

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

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/600221.html

(0)
上一篇 2021年5月9日
下一篇 2021年5月9日

精彩推荐