Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.



In the example below, it is shown how to replace negative values
by zero. The predicate is if an integer is less then zero.
Technical facts
Operating system(s) or programming environment(s)
IDE(s):
Project type:
C++ standard:
Compiler(s):
Libraries used:
STL: GNU ISO C++ Library, version 4.9.2
Qt project file: ./CppPredicateExample1/CppPredicateExample1.pro
./CppPredicateExample1/main.cpp
#include <algorithm>
#include <cassert>
#include <numeric>
#include <vector>
void ReplaceNegativeByZero(std::vector<int>& v)
{
std::replace_if(v.begin(),v.end(),
std::bind2nd(std::less<int>(),0),0);
}
int main()
{
const std::vector<int> input { -1, 1,-1, 1 };
const std::vector<int> expected { 0, 1, 0, 1 };
std::vector<int> result { input };
ReplaceNegativeByZero(result);
assert(result == expected);
}
|
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.

This page has been created by the tool CodeToHtml