[英]Converting std::pair of iterators to boost::iterator_range
I have a std::multimap
, and I want to create a boost::iterator_range
from equal_range
. 我有一个std::multimap
,我想从equal_range
创建一个boost::iterator_range
。 I found no simple way of doing it in the documentation, so I tried the following: 我在文档中找不到简单的方法,所以我尝试了以下方法:
typedef std::multimap<int, std::string> Map;
Map map;
...
boost::iterator_range<Map::iterator> r(map.equal_range(2));
Surprisingly, it works (using GCC 4.1.2). 令人惊讶的是,它的工作原理(使用GCC 4.1.2)。 I am curious how it works. 我很好奇它是如何工作的。 I found no overload for iterator_range
constructor that would do it, and multimap::iterator_range
obviously has no overload that would return Boost ranges. 我没有找到iterator_range
构造函数的重载,而multimap::iterator_range
显然没有返回Boost范围的重载。
iterator_range_core.hpp
: iterator_range_core.hpp
:
//! Constructor from a Range
template< class Range >
iterator_range( const Range& r ) :
m_Begin( impl::adl_begin( r ) ), m_End( impl::adl_end( r ) )
{}
impl::adl_begin
takes you to boost::begin
. impl::adl_begin
boost::begin
。 Having a look at begin.hpp
we see (among other voodoo ): 看看begin.hpp
我们看到(除了其他伏都 begin.hpp
):
template< typename Iterator >
inline Iterator range_begin( const std::pair<Iterator,Iterator>& p )
{
return p.first;
}
And for an example how types can be “adapted” into ranges have a look here (they use pair
as an example). 例如,如何将类型“调整”到范围中( 这里使用pair
作为示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.