1

我知道该怎么做:

for(auto it = mmap.begin(), end = mmap.end(); it != end; it = mmap.upper_bound(it->first))

但这不起作用:

for(auto it = mmap.rbegin(), end = mmap.rend(); it != end; it = mmap.lower_bound(it->first))

给予: error: no match for 'operator=' in 'it = mmap.std::multimap<_Key, _Tp, _Compare, _Alloc>::lower_bound<unsigned int, long int, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, long int> > >((* & it.std::reverse_iterator<_Iterator>::operator-><std::_Rb_tree_iterator<std::pair<const unsigned int, long int> > >()->std::pair<const unsigned int, long int>::first))'

4

1 回答 1

3

Astd::multimap::iterator不能直接转换为 a std::reverse_iterator。您需要使std::lower_bound基迭代器的结果为it

typedef ... multimap_type;
typedef std::reverse_iterator<multimap_type::iterator> reverse_iterator;

for (auto it = mmap.rbegin(),
          end = mmap.rend();
     it != end;
     it = reverse_iterator(mmap.lower_bound(it->first)))
{
  // ...
}

该表达式reverse_iterator(mmap.lower_bound(it->first))将构造 astd::reverse_iterator并将其结果lower_bound作为其基迭代器。

于 2013-03-23T21:34:59.840 回答