迭代equal_range结果集

时间:2013-03-16 02:14:30

标签: c++ multimap unordered-map

我有以下无序多图:

std::tr1::unordered_multimap<unsigned int, unsigned int > duplicates;

我尝试使用

获取密钥的值
std::pair<std::tr1::unordered_multimap<unsigned int, unsigned int>,std::tr1::unordered_multimap<unsigned int, unsigned int> > range = duplicates.equal_range(id);

然后使用for循环来获得多个结果:

for (std::tr1::unordered_multimap<unsigned int, unsigned int>::iterator it=range.first; it!=range.second; ++it)

但是我收到以下错误:

ref_impl/core.cpp:306:84: error: conversion from 'std::tr1::unordered_multimap<unsigned int, unsigned int>' to non-scalar type 'std::tr1::_Hashtable<unsigned int, std::pair<const unsigned int, unsigned int>, std::allocator<std::pair<const unsigned int, unsigned int> >, std::_Select1st<std::pair<const unsigned int, unsigned int> >, std::equal_to<unsigned int>, std::tr1::hash<unsigned int>, std::tr1::__detail::_Mod_range_hashing, std::tr1::__detail::_Default_ranged_hash, std::tr1::__detail::_Prime_rehash_policy, false, false, false>::iterator {aka std::tr1::__detail::_Hashtable_iterator<std::pair<const unsigned int, unsigned int>, false, false>}' requested
ref_impl/core.cpp:306:101: error: no match for 'operator!=' in 'it != range.std::pair<std::tr1::unordered_multimap<unsigned int, unsigned int>, std::tr1::unordered_multimap<unsigned int, unsigned int> >::second'

有什么建议吗?

2 个答案:

答案 0 :(得分:3)

unordered_multimap::equal_range返回一对迭代器,而不是一对unordered_multimap。将第二行更改为以下内容,一切都应该有效:

typedef std::tr1::unordered_multimap<unsigned int, unsigned int>::iterator iterator;
std::pair<iterator, iterator> range = duplicates.equal_range(id);

答案 1 :(得分:1)

您已将range声明为一对无序映射,而不是无序映射迭代器。感谢上帝为汽车。你确定你不能使用它吗?它会让你的生活变得如此简单:

auto range = duplicates.equal_range(id);

否则,你应该自由使用typedef