[英]C++: no match for operator< when trying to iterate through boost::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> >]()
尝试:
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];
}
您不能使用<
因为迭代器指向内存,并且不能保证内存是连续的。 这就是为什么您必须使用!=
比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.