boost::make_iterator_range

References

Headers

boost::make_iterator_range is available by including any of the following headers:

  • boost/range/iterator_range.hpp or
  • boost/range.hpp or
  • boost/range/adaptors.hpp or
  • boost/range/algorithm.hpp or
  • boost/range/istream_range.hpp or
  • boost/range/atl.hpp or
  • boost/range/mfc.hpp or
  • boost/range/join.hpp or
  • boost/range/as_literal.hpp or
  • boost/range/combine.hpp or
  • boost/range/as_array.hpp or
  • boost/range/irange.hpp or
  • boost/range/sub_range.hpp or
  • boost/range/algorithm_ext.hpp

Examples

iterator_range.cpp

#include <iostream>
#include <iterator>
#include <sstream>

#include <boost/range.hpp>


void iterator_range_demo() {
    std::stringstream ss("The quick brown fox jumps over the lazy dog.");
    auto begin = std::istream_iterator<std::string>(ss);
    auto end = std::istream_iterator<std::string>();

    // iterator_range() creates a range object from a begin and end iterator.
    // Since ranges are constructed implicitly from containers that have
    // .begin() and .end() methods, it's mostly useful for wrapping custom
    // iterators (e.g. turning a database_row_iterator into a range).
    //
    // Note: Contrived example, use istream_range in production code.
    auto range = boost::iterator_range<decltype(begin)>(begin, end);
    for (const auto & word : range) {
        std::cout << "[" << word << "] ";
    }
    std::cout << std::endl;
}

void make_iterator_range_demo() {
    std::stringstream ss("The quick brown fox jumps over the lazy dog.");
    auto begin = std::istream_iterator<std::string>(ss);
    auto end = std::istream_iterator<std::string>();

    // In practice, it's more convenient to use the make_iterator_range()
    // function that deduces the template type automatically.
    for (const auto & word : boost::make_iterator_range(begin, end)) {
        std::cout << "[" << word << "] ";
    }
    std::cout << std::endl;
}

int main() {
    iterator_range_demo();
    make_iterator_range_demo();

    return 0;
}

Output:

[The] [quick] [brown] [fox] [jumps] [over] [the] [lazy] [dog.] 
[The] [quick] [brown] [fox] [jumps] [over] [the] [lazy] [dog.]

 

Boost Range for Humans

This reference is part of Boost Range for Humans. Click the link to the overview.