boost::adaptors::replace

References

Headers

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

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

Examples

replaced-function.cpp

#include <iostream>
#include <vector>

#include <boost/range/adaptors.hpp>

const std::string str = "Boboo";


bool is_b_ignorecase(char c) {
    return c == 'b' || c == 'B';
}

int main() {
    // replace() is a straight-forward, element-wise replace operation.
    // All elements equal to 'o' are replaced with 'a'.
    // To get dynamic control over the replacement string, use transform().
    std::cout << "Replace 'o' in \"" << str << "\": "
              << (boost::adaptors::replace(str, 'o', 'a'))
              << std::endl;

    // replace_if() still has a constant replacement value, but whether a
    // substitution should be performed is determined by a predicate.
    std::cout << "Replace upper and lower case 'b's in \"" << str << "\": "
              << (boost::adaptors::replace_if(str, is_b_ignorecase, 'j'))
              << std::endl;

    return 0;
}

Output:

Replace 'o' in "Boboo": Babaa
Replace upper and lower case 'b's in "Boboo": jojoo

 

Boost Range for Humans

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