boost::adaptors::strided

References

Headers

boost::adaptors::strided is available by including any of the following headers:

  • boost/range/adaptor/strided.hpp or
  • boost/range/adaptors.hpp

Examples

slice_stride-pipe.cpp

#include <iostream>
#include <map>
#include <string>

#include <boost/range/adaptors.hpp>

const std::string str = "abcde";


int main() {
    std::cout << "\"" << str << "\" sliced to (1, 4): "
              << (str | boost::adaptors::sliced(1, 4))
              << std::endl;
    std::cout << "\"" << str << "\" | strided(2): "
              << (str | boost::adaptors::strided(2))
              << std::endl;

    return 0;
}

Output:

"abcde" sliced to (1, 4): bcd
"abcde" | strided(2): ace

 

istream_range.cpp

#include <iostream>
#include <sstream>

#include <boost/range/adaptors.hpp>
#include <boost/range/istream_range.hpp>


int main() {
    // istream_range() turns an input iterator into a range object.
    // The template argument type defines the type read from the stream.
    // See std::istream_iterator for more information.
    std::istringstream ss("The quick brown fox jumps over the lazy dog.");
    auto input_range = boost::istream_range<std::string>(ss);
    for (const auto &input : input_range | boost::adaptors::strided(2)) {
        std::cout << "[" << input << "] ";
    }
    std::cout << std::endl;

    std::istringstream ss2("8 2");
    auto input_range2 = boost::istream_range<int>(ss2);
    for (int input : input_range2 | boost::adaptors::replaced(8, 4)) {
        std::cout << input;
    }
    std::cout << std::endl;

    return 0;
}

Output:

[The] [brown] [jumps] [the] [dog.] 
42

 

Boost Range for Humans

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