Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

c++ - Boost Range Library: Traversing Two Ranges Sequentially

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:

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
        }
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...