2015-02-04 2 views
0

기본적으로 multi_index_container의 항목은 첫 번째 인덱스로 정렬됩니다.부스트 multiindex 기본 요소 순서 지정

예 :

typedef multi_index_container< 
MyStruct, 
indexed_by< 
    ordered_unique<member< MyStruct, int, &MyStruct::id> >, 
    ordered_non_unique<member< MyStruct, int, &MyStruct::salary> > 
> 
> MyStructsContainer; 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    MyStructsContainer myStructsContainer; 

    MyStructsContainer::iterator it1 = myStructsContainer.emplace(MyStruct{ 1, 100 }).first; 
    assert(distance(it1, myStructsContainer.end()) == 1); 
    MyStructsContainer::iterator it2 = myStructsContainer.emplace(MyStruct{ 2, 20 }).first; 
    assert(distance(it1, myStructsContainer.end()) == 2); 
} 

내 질문입니다 - 이것은 문서화는 "잘 알려진"행동하고, 내가 의지 할 수 있습니다? 아니면 그냥 부작용입니까?

구조체 목록 "MyStruct"가 있으며 ID로 정렬해야합니다. 하지만 어떤 이유로 정렬 된 큐에서 숫자 인덱스를 알아야합니다. 이 접근 방식을 사용할 수 있다면 1 차로 정렬 된 인덱스를 찾은 다음 std :: distance를 찾을 필요가 없습니다.

덕분에, 칼린

답변

0

예, 당신은에 의존 할 수 있습니다. docs에서 :

인덱스 # 0의 기능이 get<0>()를 사용하지 않고 multi_index_container 객체로부터 직접 액세스 할 수

: 예를 들어,이 es.begin()es.get<0>().begin() 동일하다.

+0

재미있는 것은 내가 [이미 대답]입니다 (http://stackoverflow.com/questions/28309136/boostmulti-index-container-find-index-iterator-from-other-index-iterator#comment44982530_28309574) 다른 날. OP가 질문에 대한 찬성을 어쨌든 업그레이드 한 것 같습니다 ... – sehe