結構體作爲map的key或放入set中,需要重載

結構體作爲map的key或放入set中,需要重載<運算符,如下:

typedef struct tagRoadKey
{
    int m_i32Type;
    int m_i32Scale;

    bool operator <(const tagRoadKey& other) const // 注意是const函數!!
    {
        if (m_i32Type != other.m_i32Type) // 類型按升序排序
        {
            return (m_i32Type < other.m_i32Type);
        }
        else // 如果類型相同,按比例尺升序排序
        {
            return (m_i32Scale < other.m_i32Scale);
        }
    }

} RoadKey;

也可以重載>運算符,示例如下:

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

using namespace std;

class Array
{
private:
    int m_i32Num1;
    int m_i32Num2;

public:
    Array(int i32Num1, int i32Num2);
    bool operator >(const Array& other) const;
};

Array::Array(int i32Num1, int i32Num2)
{
    m_i32Num1 = i32Num1;
    m_i32Num2 = i32Num2;
}

bool Array::operator >(const Array& other) const
{
    if (m_i32Num1 > other.m_i32Num1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

// 此結構體作爲map的value
struct TInfo
{
    int m_i32Num1;
    int m_i32Num2;
};

int main(int argc, char* argv[])
{
    map<Array, TInfo, greater<Array> > stMap;

    TInfo stInfo1 = { 1, 1};
    stMap.insert(pair<Array, TInfo>(Array(1, 2), stInfo1));

    TInfo stInfo2 = { 2, 1, 1 };
    stMap.insert(pair<Array, TInfo>(Array(2, 2), stInfo2));

    TInfo stInfo3 = { 3, 1, 1 };
    stMap.insert(pair<Array, TInfo>(Array(3, 2), stInfo3));

    for (map<Array, TInfo, greater<Array> >::iterator it = stMap.begin(); it != stMap.end(); ++it)
    {
        cout << it->second.m_i32Num1 << endl;
    }

    return 0;
}

說明:
 map缺省是用less<Key>作爲比較器,所以它要求作爲Key的類要重載“<”操作符,沒有重載“<”操作符,而是重載了“>”操作符就會報錯。
反之,也可以顯式地用greater<Key>作爲比較器,此時就必要重載Key類中的“>”操作符了。

附:stl中map和set的聲明,二者比較像,底層都是用紅黑樹實現的

template < class Key, class Compare = less<Key>,
           class Allocator = allocator<Key> > class set;

template < class Key, class T, class Compare = less<Key>,
           class Allocator = allocator<pair<const Key,T> > > class map;
 
template < class Key, class Compare = less<Key>,
           class Allocator = allocator<Key> > class multiset;
 
template < class Key, class T, class Compare = less<Key>,
           class Allocator = allocator<pair<const Key,T> > > class multimap;
 
從上面的聲明可以看出,也可以定義一個函數對象Compare,聲明map或set類型時傳進入,如:

struct TTimeCompare
{
    bool operator ()(const CTimerEvent* po1, const CTimerEvent* po2)const
    {        
        return (po1->m_oNextTick < po2->m_oNextTick);
    }
};

typedef multiset<CTimerEvent*, TTimeCompare> TEventSet;


struct ltstr // less than
{
    bool operator()(const char* s1, const char* s2) const
    {
        return strcmp(s1, s2) < 0;
    }
};




set<const char*, ltstr> stSet; // set<Key, Compare, Alloc>
map<const char*, int, ltstr> stMap; // map<Key, Data, Compare, Alloc>




struct eqstr // equal
{
    bool operator()(const char* s1, const char* s2) const
    {
        return strcmp(s1, s2) == 0;
    }
};


hash_map<const char*, int, hash<const char*>, eqstr> stHashMap;  // hash_map<Key, Data, HashFcn, EqualKey, Alloc>


// 自定義hash函數
namespace std
{
    template<>
    struct hash<KEY_TYPE>
    {
        size_t operator()(const KEY_TYPE& key) const
        {
            //return key.Hash();
        }
    };
}


發佈了43 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章