Skip to content

Latest commit

 

History

History
38 lines (28 loc) · 1.67 KB

not1_unary_negate.md

File metadata and controls

38 lines (28 loc) · 1.67 KB

Understanding std::not1 and std::unary_negate

What are they?

  • std::not1: This is a helper function that creates a function object which negates the result of another unary predicate. It's essentially a logical NOT for functions.
  • std::unary_negate: This is a function object wrapper that inverts the result of the wrapped unary predicate. It's the underlying type returned by std::not1.

Simple Example

Let's say we have a vector of integers and we want to count how many numbers are greater than or equal to 5.

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

int main() {
    std::vector<int> numbers = {2, 5, 8, 1, 7, 4};

    // Using std::not1 and lambda
    auto is_less_than_5 = [](int x) { return x < 5; };
    int count_greater_or_equal_to_5 = std::count_if(numbers.begin(), numbers.end(), std::not1(is_less_than_5));

    std::cout << "Count of numbers greater than or equal to 5: " << count_greater_or_equal_to_5 << std::endl;

    return 0;
}

Explanation

  1. We create a lambda expression is_less_than_5 that checks if a number is less than 5.
  2. We use std::not1 to negate the result of is_less_than_5. This effectively creates a predicate that checks if a number is greater than or equal to 5.
  3. We use std::count_if to count the number of elements in the vector that satisfy the negated predicate.

Key Points

  • std::not1 is a convenience function to create a negated predicate.
  • std::unary_negate is the underlying type and can be used directly, but std::not1 is often more convenient.
  • These functions are useful when you need to invert the logic of an existing predicate without rewriting it.