0

我发现,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;
}

这很好用,但我也想使用反向迭代。所以我补充说:

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

在此之后它不会编译,有没有人可以告诉如何在无序地图上使用反向 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 ==========
4

1 回答 1

6

简而言之,您不会反向迭代unordered_map

unordered_map不提供任何命令。因此,迭代容器会导致任意顺序。如果可以反向迭代它,它将产生同样任意的顺序。如果应用程序需要反向迭代 a unordered_map,则应用程序依赖于顺序,并且不能安全地使用unordered_map. 因此,应考虑替代容器类型。例如:

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

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

于 2013-06-05T13:43:19.530 回答