Boost范围库:按顺序遍历两个范围 [英] Boost Range Library: Traversing Two Ranges Sequentially

查看:120
本文介绍了Boost范围库:按顺序遍历两个范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Boost范围库( http://www.boost。 org / doc / libs / 1_35_0 / libs / range / index.html )允许我们将一对迭代器抽象为一个范围。现在我想将两个范围合并为一个,即:

Boost range library (http://www.boost.org/doc/libs/1_35_0/libs/range/index.html) allows us to abstract a pair of iterators into a range. Now I want to combine two ranges into one, viz:

给定两个范围r1和r2,定义r遍历[r1.begin(),r1.end [然后[r2.begin(),r2.end()[。有什么方法可以使用r1和r2将r定义为范围?

given two ranges r1 and r2, define r which traverses [r1.begin(), r1.end()[ and then [r2.begin(), r2.end()[. Is there some way to define r as a range using r1 and r2?

推荐答案

。有一种方法使用boost / range / join.hpp来连接两个范围。不幸的输出范围类型不包括在界面中:

I needed this again so I had a second look. There is a way to concat two ranges using boost/range/join.hpp. Unluckily the output range type is not included in the interface:

#include "boost/range/join.hpp"
#include "boost/foreach.hpp"
#include <iostream>

int main() {
        int a[] = {1, 2, 3, 4};
        int b[] = {7, 2, 3, 4};

        boost::iterator_range<int*> ai(&a[0], &a[4]);
        boost::iterator_range<int*> bi(&b[0], &b[4]);
        boost::iterator_range<
           boost::range_detail::
           join_iterator<int*, int*, int, int&, 
           boost::random_access_traversal_tag> > ci = boost::join(ai, bi); 

        BOOST_FOREACH(int& i, ci) {
                std::cout << i; //prints 12347234
        }
}

编译器消息。 C ++ 0x auto 也会在那里相关。

I found the output type using the compiler messages. C++0x auto will be relevant there as well.

这篇关于Boost范围库:按顺序遍历两个范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆