2010-03-18 128 views
1

以前,我使用STL地圖來執行上述任務。按照字符串長度排序CMap關鍵字

struct ltstr 
{ 
    bool operator()(std::string s1, std::string s2) const 
    { 
     const int l1 = s1.length(); 
     const int l2 = s2.length(); 
     if (l1 == l2) { 
      // In alphabetical order. 
      return s1.compare(s2) < 0; 
     } 
     // From longest length to shortest length. 
     return l1 > l2; 
    } 
}; 
std::map<std::string, int, ltstr> m; 

如何使用CMap執行相同的任務?

// How to make key sorted by string length? 
CMap<CString, LPCTSTR, int, int> m; 
+1

我很確定'CMap'實際上是一個哈希映射,因此不提供排序。 – GManNickG 2010-03-18 03:39:59

回答

4

你不能。從the MSDN documentation for CMap

你可能認爲這個迭代是按鍵值順序的;不是這樣。檢索到的元素的序列是不確定的。

+0

我投降!!!! – 2010-03-18 07:15:18

1

在地圖的順序由散列值來決定,並且是所有意圖和目的... 隨機

相反,你可能想要保持/生成一個指向鍵的指針列表或類似的東西。