mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-17 01:54:36 +03:00
Add std::not_fn.
This commit is contained in:
14
CPP17.md
14
CPP17.md
@@ -36,6 +36,7 @@ C++17 includes the following new library features:
|
||||
- [std::reduce](#stdreduce)
|
||||
- [prefix sum algorithms](#prefix-sum-algorithms)
|
||||
- [gcd and lcm](#gcd-and-lcm)
|
||||
- [std::not_fn](#stdnot_fn)
|
||||
|
||||
## C++17 Language Features
|
||||
|
||||
@@ -600,6 +601,19 @@ std::gcd(p, q); // == 3
|
||||
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
|
||||
* [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.
|
||||
|
||||
14
README.md
14
README.md
@@ -66,6 +66,7 @@ C++17 includes the following new library features:
|
||||
- [std::reduce](#stdreduce)
|
||||
- [prefix sum algorithms](#prefix-sum-algorithms)
|
||||
- [gcd and lcm](#gcd-and-lcm)
|
||||
- [std::not_fn](#stdnot_fn)
|
||||
|
||||
C++14 includes the following new language features:
|
||||
- [binary literals](#binary-literals)
|
||||
@@ -1264,6 +1265,19 @@ std::gcd(p, q); // == 3
|
||||
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
|
||||
|
||||
### Binary literals
|
||||
|
||||
Reference in New Issue
Block a user