boost::adaptors::copy

References

Headers

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

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

Examples

copied-function.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() {
    // copy() 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 = boost::adaptors::copy(str, 0, 4);
    std::vector<int> newvec = boost::adaptors::copy(vec, 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.