Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) std::logical_not

 

std::logical_not is a functor that performs a operator! on an element.

 

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

int main()
{
  std::vector<bool> v; //Prefer std::bitset of boost::dynamic bitset
  v.push_back(true);
  v.push_back(false);

  assert(v.size() == 2);
  assert(v[0] == true );
  assert(v[1] == false);

  std::vector<bool> w; //Prefer std::bitset of boost::dynamic bitset

  //Use std::logical_not on the elements of v,
  //append the result to w
  std::transform(
    v.begin(),
    v.end(),
    std::back_inserter(w),
    std::logical_not<bool>());

  //Check if std::logical_not works as expected
  assert(w.size() == 2);
  assert(w[0] == false);
  assert(w[1] == true );
}

 

 

 

 

 

External links

 

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict