Search code examples
c++multimapunordered-map

Iterate over equal_range result set


I have the following unordered multimap:

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

And I try to get the values of a key using

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

And then using a for loop to get the multiple results:

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

But I get the following error:

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'

Any suggestions?


Solution

  • unordered_multimap::equal_range returns a pair of iterators, not a pair of unordered_multimaps. Change the second line to the following, and everything should work:

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