Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

std::not_fn

Use case

Negate predicates.

Explanation

std::not_fn wraps a callable and negates its result.

Code

#include <algorithm>
#include <cstdint>
#include <functional>
#include <iostream>
#include <vector>

int main() {
  std::vector<uint64_t> addrs{0x1000, 0x0, 0x2000, 0x0, 0x3000};

  auto isZero = [](uint64_t a) { return a == 0; };

  auto count = std::count_if(addrs.begin(), addrs.end(), std::not_fn(isZero));

  std::cout << "Non-zero: " << count << "\n";

  return 0;
}

View on GitHub.

Output

$ ./src/c++17/build/std-not_fn
Non-zero: 3