2012-10-24 2 views
1

나는 다음과 같은 코드가 있습니다C++ : 연산자에 대한 어떤 경기는 <시 부스트 :: unordered_map도 반복하려고 <string,int>

boost::unordered_map<std::string, int> map; 
map["hello"]++; 
map["world"]++; 

for(boost::unordered_map<std::string, int>::iterator it = map.begin(); it < map.end(); it++){ 
    cout << map[it->first]; 
} 

을 내가하려고하면 다음과 같은 오류를 얻을 수 있지만, 아무 생각도 이유가없는 컴파일?

error: no match for ‘operator<’ in ‘it < map.boost::unordered::unordered_map<K, T, H, P, A>::end [with K = std::basic_string<char>, T = int, H = boost::hash<std::basic_string<char> >, P = std::equal_to<std::basic_string<char> >, A = std::allocator<std::pair<const std::basic_string<char>, int> >, boost::unordered::unordered_map<K, T, H, P, A>::iterator = boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<const std::basic_string<char>, int> >*, std::pair<const std::basic_string<char>, int> >]() 
+1

'it! = map.end()'를 사용하십시오. – sje397

답변

4

시도 : (대신 it < map.end()에) for 루프 종료 조건으로

it != map.end() 

. 당신은 메모리에 < 때문에 반복자 포인트를 사용하지 못할

boost::unordered_map<std::string, int>::iterator it = map.begin(); 
for(; it != map.end(); ++it){ 
    cout << map[it->first]; 
} 

, 당신은 그 메모리가 연속이다 보장하지 못할 :

+0

Aly

+0

@aly 컴파일러가 말했듯이'operator <'가 정의되어 있지 않기 때문에 : – sje397

+1

...지도가 잘 정렬되어 있지 않기 때문에. :) – Reunanen

3

는 반복자의 경우에는 != 연산자를 사용해야합니다. 그래서 !=을 사용해야합니다.

+0

예를 들어 벡터 반복자는 연산자 <를 정의하므로이 종료 조건을 사용할 수 있습니다. 반복기가 어떻게 구현되는지에 따라 다르다. – Aly

+0

Iterator는 인터페이스이다! = 항상 받아 들일 수있다 < –

+0

http://msdn.microsoft.com/en-us/library/54skkak1.aspx – Reunanen