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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章