[英]boost unordered_map reverse foreach
我发现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 ==========
简而言之,您不必反向遍历unordered_map
。
unordered_map
提供顺序。 因此,在容器上进行迭代会导致任意顺序。 如果可以反向迭代它,它将产生同样任意的顺序。 如果应用程序需要反向遍历unordered_map
,则该应用程序依赖于顺序,因此无法安全地使用unordered_map
。 因此,应考虑其他容器类型。 例如:
std::vector<std::pair<std::string, int>>
为了扩展编译器错误,Boost.ForEach尝试获取rbegin并重载迭代器。 最后,Boost.ForEach将尝试使用boost::reverse_iterator
来适应Sequence :: iterator ,要求适应的迭代器是双向迭代器 。 虽然boost::unordered_map::iterator
器类型是实现定义的,但它至少必须是正向迭代器 。 因此,当reverse_iterator
递增时,它将递减基础迭代器,从而导致编译器错误,因为Sequence :: iterator不提供一元递减运算符( --
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.