gpt4 book ai didi

c++ - 如何为唯一键向后迭代 multimap ?

转载 作者:行者123 更新时间:2023-11-28 03:20:29 24 4
gpt4 key购买 nike

我知道如何向前:

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))'

最佳答案

std::multimap::iterator 不能直接转换为 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)) 将构造一个 std::reverse_iterator,其结果为 lower_bound作为它的基础迭代器。

关于c++ - 如何为唯一键向后迭代 multimap ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15592243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com