提升unordered_map反向foreach

ST3*_*ST3 0 c++ iteration reverse boost unordered-map

我发现,BOOST_REVERSE_FOREACH与BOOST_FOREACH的工作方式不同.

我的代码:

#include <boost\unordered_map.hpp>
#include <boost\foreach.hpp>
#include <iostream>
#include <string>

typedef boost::unordered_map<std::string, int> map;

int main()
{
    map MyMap;

    MyMap["two"] = 2;
    MyMap["three"] = 3;
    MyMap["one"] = 1;

    std::cout << MyMap["one"] << MyMap["two"] << MyMap["three"] << std::endl;

    BOOST_FOREACH (map::value_type value, MyMap)
    {
        std::cout << value.second;
    }
    std::cout << std::endl;

    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这工作正常,但我也想使用反向迭代.所以我补充说:

BOOST_REVERSE_FOREACH (map::value_type value, MyMap)
{
    std::cout << value.second;
}
std::cout << std::endl;
Run Code Online (Sandbox Code Playgroud)

在此之后它无法编译,是否有人可以告诉如何在无序地图上使用反向foreach.

编译器给出:

1>c:\boost_1_52_0\boost\iterator\reverse_iterator.hpp(45): error C2675: unary '--' : 'boost::unordered::iterator_detail::iterator<NodePointer,Value>' does not define this operator or a conversion to a type acceptable to the predefined operator
1>          with
1>          [
1>              NodePointer=boost::unordered::detail::ptr_node<std::pair<const std::string,int>> *,
1>              Value=std::pair<const std::string,int>
1>          ]
1>          c:\boost_1_52_0\boost\iterator\reverse_iterator.hpp(45) : while compiling class template member function 'void boost::reverse_iterator<Iterator>::increment(void)'
1>          with
1>          [
1>              Iterator=boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<const std::string,int>> *,std::pair<const std::string,int>>
1>          ]
1>          c:\boost_1_52_0\boost\iterator\iterator_facade.hpp(520) : see reference to function template instantiation 'void boost::reverse_iterator<Iterator>::increment(void)' being compiled
1>          with
1>          [
1>              Iterator=boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<const std::string,int>> *,std::pair<const std::string,int>>
1>          ]
1>          c:\boost_1_52_0\boost\foreach.hpp(266) : see reference to class template instantiation 'boost::reverse_iterator<Iterator>' being compiled
1>          with
1>          [
1>              Iterator=boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<const std::string,int>> *,std::pair<const std::string,int>>
1>          ]
1>          c:\users\t3\documents\projects\boost unorderedmap test\boost unorderedmap test\main.cpp(25) : see reference to class template instantiation 'boost::foreach_detail_::auto_any<T>' being compiled
1>          with
1>          [
1>              T=boost::reverse_iterator<boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<const std::string,int>> *,std::pair<const std::string,int>>>
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Run Code Online (Sandbox Code Playgroud)

Tan*_*ury 6

简而言之,您不会unordered_map反过来迭代.

unordered_map不提供订单.因此,迭代容器导致任意顺序.如果可以反过来迭代它,它将产生一个同样任意的顺序.如果应用程序需要unordered_map反向迭代,那么应用程序依赖于顺序,并且无法安全地使用unordered_map.因此,应考虑替代容器类型.例如:

  • 如果插入顺序很重要并且从未发生过查找,那么请使用 std::vector<std::pair<std::string, int>>
  • 如果插入顺序很重要,并且确实发生了查找,那么请考虑Boost.MultiIndex.

为了扩展编译器错误,Boost.ForEach尝试获取rbegin和rend迭代器.最终,Boost.ForEach会尽量去适应序列:迭代boost::reverse_iterator,需要适应的迭代器是双向迭代器.虽然boost::unordered_map::iterator迭代器类型是实现定义的,但它必须至少是一个前向迭代器.因此,当reverse_iterator递增时,它会递减底层迭代器,从而导致编译器错误,因为Sequence :: iterator不提供一元递减运算符(--).