Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) PredicateExample1

 

STLQt CreatorLubuntu

 

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:

 

 

 

 

 

Qt project file: ./CppPredicateExample1/CppPredicateExample1.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ -Werror
SOURCES += main.cpp

 

 

 

 

 

./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.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml