boost::replace_copy_if

References

Headers

boost::replace_copy_if is available by including any of the following headers:

  • boost/range/algorithm/replace_copy_if.hpp or
  • boost/range/algorithm.hpp

Examples

replace_copy_if.cpp

#include <functional>
#include <iostream>
#include <vector>
#include <boost/range/algorithm.hpp>

bool is_3_or_4(int i) {
    return i == 3 || i == 4;
}

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    std::vector<int> output;

    // replace_copy_if() wraps std::replace_copy_if().
    // Returns an iterator to the end of the output range.
    boost::replace_copy_if(vec, std::back_inserter(output), is_3_or_4, 0);

    std::cout << "input vector after replace_copy_if(): ";
    boost::range::copy(vec, std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;

    std::cout << "output vector after replace_copy_if(): ";
    boost::range::copy(output, std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;
    return 0;
}

Output:

input vector after replace_copy_if(): 1 2 3 4 5 
output vector after replace_copy_if(): 1 2 0 0 5

 

Boost Range for Humans

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