Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
std::replace_if is an STL algorithm to replace elements of a sequence (on a std::vector, for example) that satisfy a certain predicate. Use std::replace if no predicate is needed (that is: a certain value must always be replaced).
Prefer algorithms over hand-written loops [1-3]. Exercise #9: No for-loops shows how to remove hand-written loops.
The code below shows how to replace all values that are negative to zero:
#include <algorithm> |
Simplified and modified from the STL that ships with C++ Builder 6.0:
template |
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.