2014-10-19 2 views
1

C++ STL Map에서 lowerbound()를 사용하려고했습니다. I 사용하기 전에, I는 다음과 같은 과정을 통해 그 기능을 테스트 I 1 테스트시 C++ map lowerbound()

int main() 
{ 
    std::map<int,int> mymap; 
    std::map<int,int>::iterator itlow; 

    mymap[1]=20; 
    mymap[3]=60; 
    mymap[4]=80; 
    mymap[5]=100; 

    itlow=mymap.lower_bound (2); 

    //Test1 
    std::cout<<(--itlow)->first<<'\n'; //print 1 
    std::cout<<itlow->second<<'\n'; //print 20 

    //Test2 
    std::cout<<(--itlow)->first<<": "<<itlow->second<<'\n'; //print 1 : 60   
} 

내가 의미하는 별도 1 및 2를 테스트 I는 Test2를 댓글과 역방향 동일. 테스트 1의 결과가 예상보다 낮지 만, Test2가 20 대신 두 번째 필드 60을 인쇄하는 이유를 모르겠습니다.

+0

테스트 2에는 정의되지 않은 동작이 있습니다. – chris

+1

좀 더 구체적으로 말씀해 주시겠습니까? 감사! – diane

+0

[예제보기] (http://stackoverflow.com/questions/949433/why-are-these-constructs-undefined-behavior) 많은 예제가 있습니다. [이] (http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points?rq=1)도 있습니다. – chris

답변

2

itlow->second 전후에 (--itlow)->first이 평가되는지 여부는 불특정입니다. 이전에 평가 한 경우 20이됩니다. 그렇지 않으면 60이됩니다.

order of evaluation of operands을 참조하십시오.

+0

시퀀스 포인트 사이에서 'itlow' "를 읽고 쓸 수 없습니다. – chris

+0

@chris : 그래서 당신은 그것이 불특정하지 않고 정의되지 않았다고 말하고 있습니까? – NPE

+0

[SO] (http://stackoverflow.com/questions/3690141/multiple-preincrement-operations-on-a-variable-in-cc/3691469#3691469) 글쎄, 내가 생각하는 것 같아요, 그것은 사전입니다 -C++ 11 이후. 상황이 미묘하게 바뀔 수있는 방법은 항상 재미 있습니다. – chris