Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) std::not1

 

std::not1 is an STL adapter to negate a predicate derived from std::unary_function. To negate a predicate derived from std::binary_function, use std::not2.

 

 

 

 

 

Example

 

Operating system: Ubuntu 10.04 LTS Lucid Lynx

IDE: Qt Creator 2.0.0

Project type: Qt4 Console Application

Compiler: G++ 4.4.1

Libraries used:

 

#include <algorithm>
#include <cassert>
#include <functional>
#include <vector>

struct IsGood : std::unary_function<int,bool>
{
  bool operator() (const int x) const
  {
    return (x == 42);
  }
};

int main ()
{
  std::vector<int> v;
  for (int i=0; i!=100; ++i) { v.push_back(i); }
  //Count the good ones
  assert(std::count_if(v.begin(),v.end(),IsGood())==1);
  //Count the bad (not-good) ones
  assert(std::count_if(v.begin(),v.end(),std::not1(IsGood()))==99);
}

 

 

 

 

 

 

External links

 

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict