2014-03-19 107 views
0

我的結構是怎樣的:如何爲包含map的結構編寫比較運算符?

struct MyStruct 
    { 

    char *name; 
    map<char*,char*> mymap;//assume that this map insert data in increasing order 
    }; 

我有另一個地圖:

map<MyStruct,int,Compare> mTest; 

    //compare function for less then operator 
    struct Compare 
    : public std::binary_function<MyStruct, MyStruct, bool> 
    { 
     bool operator()(const MyStruct &a, const MyStruct&b) 
     { 
      if(strcmp(a.name,b.name)< 0) 
       return true; 
      else if(strcmp(a.name,b.name)==0) 
      { 
       //How should I compare map `mymap` ?? 
      } 
      return false; 
     } 
    } 

所以我應該如何寫比較爲mymap

這就是我想要的東西基本上是:

兩個地圖都是平等的,如果

  1. 有名字都是平等的

  2. 他們的地圖大小相等

  3. 然後地圖的內容應該是平等的,即它們的關鍵和價值。

+0

您應該使用C++的語法,首先。 'elseif'不是C++,你的'if'缺少一個結束符。那完全取決於你如何將兩張地圖正確地排序。隨你便。唯一的是你應該確保它是一個嚴格的順序。 –

+0

@ArneMertz我已經更新了上面的代碼。讓我們假設'mymap'按遞增順序存儲數據。現在我怎麼能比較'Compare'函數中的兩個地圖? – EmptyData

+0

首先,'if(strcmp(a.name

回答

1

有幾種方法可以訂購地圖。必須考慮三件事情:地圖大小,關鍵字和值。由於地圖本身已經由鍵排序,因此比較鍵之後的值是很自然的。所以,給定兩張尺寸相同的地圖,只有鑰匙才重要。我會用整項和值的一個簡單的例子:

map1: 1->42  map2: 5->16  map3: 1->44 map4: 1->42 
     2-> 5    6->16   2->67   2-> 7 
     7-> 8    7-> 8   3->10   7-> 8 

現在,比較MAP1和MAP2很簡單:MAP1的第一個關鍵是比MAP2的第一個關鍵較低,所以MAP1應該放在第一位。
比較map1和map3會在第一個條目中給出相同的鍵值,但map1對應的值較低,因此map1會再次出現。
比較map1和map4顯示第一個鍵值對完全相同,但比較第二個對則表明map1是第一個,因爲它的值再次降低。

按尺寸排列的順序再次微不足道。較小尺寸的地圖會在較大尺寸之前顯示。

現在,完全取決於您,如果您想先按大小進行排序或先按鍵/值進行排序。 考慮額外的地圖:

map5: 5->16 
     7-> 3 

MAP5的大小爲2,MAP1的大小爲3所以,如果你按大小排序第一,MAP5來MAP1之前。如果先比較元素,則map1會出現在map5之前,因爲第一個元素較低。

此比較已在C++中提供:std::pair提供了一個operator<,它比較鍵優先和之後的值。一般元素集合的元素比較完成víastd::lexicographical_compare。另外,std::map提供了一個operator<,爲您進行字典對比。它首先比較元素,其次是大小。

然而,在你的情況,因爲你正在使用char* s,而不是C++的string S,你必須編寫自己的pair<char*, char*>這是你的地圖的元素,比較。我會使用字符串產品總數意見後,就變得非常簡單,因爲std::string提供operator<

struct MyStruct 
{ 
    string name; 
    map<string, string> mymap; 
}; 


map<MyStruct,int,Compare> mTest; 

//compare function for less then operator 
struct Compare 
{ 
    bool operator()(const MyStruct &a, const MyStruct&b) 
    { 
    return a.name < b.name //name first 
     || (a.name == b.name && cmp(a.mymap, b.mymap)); 
    } 

    bool cmp(map<string, string> const& lhs, map<string, string> const& rhs) 
    { 
    return lhs.size() < rhs.size() //size first 
     || (lhs.size() == rhs.size() && lhs < rhs); 
    } 
}; 
1

你對char*的使用是坦率的可怕和過時的。使用std::string,類變爲:

struct MyStruct 
{ 
    std::string name; 
    map<std::string, std::string> mymap; 
}; 

現在,對於地圖,您需要提供低於運營商,例如:

struct MyStruct 
{ 
    std::string name; 
    map<std::string, std::string> mymap; 

    friend bool operator<(MyStruct const& lhs, MyStruct const& rhs) 
    { 
    // Now the comparison is easier - use the defaults! 
    if (lhs.name < rhs.name) 
     return true; 
    return lhs.mymap < rhs.mymap; // this does a lexicographical comparison of all values. 
    } 
}; 

那麼你的結構圖變爲:

std::map<MyStruct, int> mTest; 

不需要繁瑣的代碼。

編輯:只注意到你的更新,你可以實現其他運營商MyStruct和撰寫他們在我從打電話的namemymap邏輯運算符上面做,你不需要實現任何自定義的黑客自己。