boost::adaptors::transform

References

Headers

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

  • boost/range/adaptor/transformed.hpp or
  • boost/range/adaptors.hpp or
  • boost/range/adaptor/map.hpp

Examples

transformed-function.cpp

#include <iostream>
#include <vector>

#include <boost/range/adaptors.hpp>

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


/** Map integers to the alphabet: 0->a, 1->b, ... */
std::string alphabetize(int i) {
    return std::string(1, 'a' + i);
}

int main() {
    // transform() is a nice and general way to change elements in a Boost
    // Range. Even type conversions work fluently, as shown here:
    std::cout << "Transform a vector<int> into letters: "
              << (boost::adaptors::transform(vec, alphabetize))
              << std::endl;

    return 0;
}

Output:

Transform a vector<int> into letters: abcdefghij

 

Boost Range for Humans

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