2010-03-18 7 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

답변

4

수 없습니다. 부터 the MSDN documentation for CMap :

이 반복은 키 값에 의해 순차라고 생각할 수 있습니다. 그렇지 않습니다. 검색된 요소의 순서는 불확정합니다.

+0

나는 항복한다 !!!! –

1

맵의 시퀀스는 해시 값에 의해 결정되고, 보나이다 ... 랜덤.

대신 키에 대한 포인터의 정렬 된 목록을 유지하거나 생성 할 수 있습니다.