2013-09-30 22 views

回答

3

一種可能的實現是這樣的:

class Person 
{ 
    string name; 
    string section; 
}; 

bool operator<(const Person& lhs, const Person& rhs) { 
    return lhs.name < rhs.name; 
} 

這將排序按字典的名字。其他順序也是可能的,這取決於你的需求。你的問題有點不清楚,因爲通常你需要兩種類型的地圖,一個鍵類型和一個值類型。該比較僅適用於鍵類型,而不適用於值類型。

3

大概就像

#include <tuple> // for std::tie 

struct ComparePersons 
{ 
    bool operator()(const Person& lhs, const Person& rhs) const 
    { 
    return std::tie(lhs.name, lhs,section) < std::tie(rhs.name, rhs.section); 
    } 
}; 

執行的逐一小於比較,使用name第一和section秒。顯然,這假定你有興趣使用Person作爲地圖的關鍵字。