boost::adaptors::replaced_if

References

Headers

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

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

Examples

replaced-pipe.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() {
    // replaced() 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 transformed().
    std::cout << "Replace 'o' in \"" << str << "\": "
              << (str | boost::adaptors::replaced('o', 'a'))
              << std::endl;

    // replaced_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 << "\": "
              << (str | boost::adaptors::replaced_if(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.