2011-11-26 3 views
0

제 질문은 this one과 거의 동일하지만 해결책이 제 오류를 해결하지 못했습니다. main.h에서표준 : : 문자열을 키로 사용하여 std :: map을 반복 할 수 없습니다.

내가 가진 :

#include <map> 
#include <string> 

std::map<std::string, int64_t> receive_times; 

그리고 main.cpp에 :

std::map<std::string, int64_t>::const_iterator iter; 
std::map<std::string, int64_t>::const_iterator eiter = receive_times.end(); 

for (iter = receive_times.begin(); iter < eiter; ++iter) 
    printf("%s: %ld\n", iter->first.c_str(), iter->second); 

을하지만, 내가 노력하고 내가 다음과 같은 오류 얻을 컴파일 할 때 :

error: invalid operands to binary expression ('std::map<std::string, int64_t>::const_iterator' (aka '_Rb_tree_const_iterator<value_type>') and 'std::map<std::string, int64_t>::const_iterator' 
    (aka '_Rb_tree_const_iterator<value_type>')) 
    for (iter = receive_times.begin(); iter < eiter; ++iter) 
            ~~~~^~~~~~ 

솔루션의를 상단에 링크 된 질문은 누락 되었기 때문입니다. #include <string>,하지만 분명히 포함되어 있습니다. 어떤 힌트?

+5

헤더 파일에 변수를 정의하면 안됩니다 ... –

답변

7

반복자는 동등성을 위해서만 관계형으로 비교할 수 없습니다. 따라서 iter != eiter라고 말하십시오. C++ 11

for (std::map<std::string, int64_t>::const_iterator iter = receive_times.begin(), 
    end = receive_times.end(); iter != end; ++iter) 
{ 
    // ... 
} 

또는 :

루프를 작성하는 덜 시끄러운 방법 (보통 typedef에 가장지도 유형을!) :

for (auto it = receive_times.cbegin(), end = receive_timed.cend(); it != end; ++it) 

또는 균등 :

for (const auto & p : receive_times) 
{ 
    // do something with p.first and p.second 
} 
+0

감사합니다. 이 문제가 해결되었습니다. – Rezzie

0

컨테이너 반복자의 관용적 루프 구조는 다음과 같습니다.

for (iter = receive_times.begin(); iter != eiter; ++iter) 
관련 문제