boost::adaptors::copied

References

Headers

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

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

Examples

copied-pipe.cpp

#include <iostream>
#include <string>
#include <vector>

#include <boost/range/adaptors.hpp>

const std::string str = "abcdef";
const std::vector<int> vec = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };


int main() {
    // copied() is great because the returned range object has a conversion
    // operator to any container that supports initialization from a begin/end
    // iterator pair.
    // Unfortunately, it takes two arguments that define a slice from the input
    // range to copy. That's nice if you want to slice, but often you don't.
    std::string newstr = str | boost::adaptors::copied(0, 4);
    std::vector<int> newvec = vec | boost::adaptors::copied(3, 6);

    std::cout << "Copied str[0:4] to new string: " << newstr << std::endl;
    std::cout << "Copied vec[3:6] to new vector: ";
    for (const int i : newvec) {
        std::cout << i << " ";
    }
    std::cout << std::endl;

    return 0;
}

Output:

Copied str[0:4] to new string: abcd
Copied vec[3:6] to new vector: 3 4 5

 

Boost Range for Humans

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