Add std::not_fn.

This commit is contained in:
Anthony Calandra
2023-02-20 21:12:27 -05:00
parent 2328e86239
commit c0e0f37bd0
2 changed files with 28 additions and 0 deletions

View File

@@ -36,6 +36,7 @@ C++17 includes the following new library features:
- [std::reduce](#stdreduce) - [std::reduce](#stdreduce)
- [prefix sum algorithms](#prefix-sum-algorithms) - [prefix sum algorithms](#prefix-sum-algorithms)
- [gcd and lcm](#gcd-and-lcm) - [gcd and lcm](#gcd-and-lcm)
- [std::not_fn](#stdnot_fn)
## C++17 Language Features ## C++17 Language Features
@@ -600,6 +601,19 @@ std::gcd(p, q); // == 3
std::lcm(p, q); // == 9 std::lcm(p, q); // == 9
``` ```
### std::not_fn
Utility function that returns the negation of the result of the given function.
```c++
const std::ostream_iterator<int> ostream_it{ std::cout, " " };
const auto is_even = [](const auto n) { return n % 2 == 0; };
std::vector<int> v{ 0, 1, 2, 3, 4 };
// Print all even numbers.
std::copy_if(std::cbegin(v), std::cend(v), ostream_it, is_even); // 0 2 4
// Print all odd (not even) numbers.
std::copy_if(std::cbegin(v), std::cend(v), ostream_it, std::not_fn(is_even)); // 1 3
```
## Acknowledgements ## Acknowledgements
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features. * [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features.
* [C++ Rvalue References Explained](http://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics. * [C++ Rvalue References Explained](http://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics.

View File

@@ -66,6 +66,7 @@ C++17 includes the following new library features:
- [std::reduce](#stdreduce) - [std::reduce](#stdreduce)
- [prefix sum algorithms](#prefix-sum-algorithms) - [prefix sum algorithms](#prefix-sum-algorithms)
- [gcd and lcm](#gcd-and-lcm) - [gcd and lcm](#gcd-and-lcm)
- [std::not_fn](#stdnot_fn)
C++14 includes the following new language features: C++14 includes the following new language features:
- [binary literals](#binary-literals) - [binary literals](#binary-literals)
@@ -1264,6 +1265,19 @@ std::gcd(p, q); // == 3
std::lcm(p, q); // == 9 std::lcm(p, q); // == 9
``` ```
### std::not_fn
Utility function that returns the negation of the result of the given function.
```c++
const std::ostream_iterator<int> ostream_it{ std::cout, " " };
const auto is_even = [](const auto n) { return n % 2 == 0; };
std::vector<int> v{ 0, 1, 2, 3, 4 };
// Print all even numbers.
std::copy_if(std::cbegin(v), std::cend(v), ostream_it, is_even); // 0 2 4
// Print all odd (not even) numbers.
std::copy_if(std::cbegin(v), std::cend(v), ostream_it, std::not_fn(is_even)); // 1 3
```
## C++14 Language Features ## C++14 Language Features
### Binary literals ### Binary literals