当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C++中自定义结构体或类作为关联容器的键

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

2020年03月16日  | 移动技术网IT编程  | 我要评论

钋怎么读,黄金城19119存100送58,想你的夜伴奏

目录

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网