Skip to content

Commit c0e0f37

Browse files
Add std::not_fn.
1 parent 2328e86 commit c0e0f37

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

CPP17.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ C++17 includes the following new library features:
3636
- [std::reduce](#stdreduce)
3737
- [prefix sum algorithms](#prefix-sum-algorithms)
3838
- [gcd and lcm](#gcd-and-lcm)
39+
- [std::not_fn](#stdnot_fn)
3940

4041
## C++17 Language Features
4142

@@ -600,6 +601,19 @@ std::gcd(p, q); // == 3
600601
std::lcm(p, q); // == 9
601602
```
602603

604+
### std::not_fn
605+
Utility function that returns the negation of the result of the given function.
606+
```c++
607+
const std::ostream_iterator<int> ostream_it{ std::cout, " " };
608+
const auto is_even = [](const auto n) { return n % 2 == 0; };
609+
std::vector<int> v{ 0, 1, 2, 3, 4 };
610+
611+
// Print all even numbers.
612+
std::copy_if(std::cbegin(v), std::cend(v), ostream_it, is_even); // 0 2 4
613+
// Print all odd (not even) numbers.
614+
std::copy_if(std::cbegin(v), std::cend(v), ostream_it, std::not_fn(is_even)); // 1 3
615+
```
616+
603617
## Acknowledgements
604618
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features.
605619
* [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.

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ C++17 includes the following new library features:
6666
- [std::reduce](#stdreduce)
6767
- [prefix sum algorithms](#prefix-sum-algorithms)
6868
- [gcd and lcm](#gcd-and-lcm)
69+
- [std::not_fn](#stdnot_fn)
6970

7071
C++14 includes the following new language features:
7172
- [binary literals](#binary-literals)
@@ -1264,6 +1265,19 @@ std::gcd(p, q); // == 3
12641265
std::lcm(p, q); // == 9
12651266
```
12661267

1268+
### std::not_fn
1269+
Utility function that returns the negation of the result of the given function.
1270+
```c++
1271+
const std::ostream_iterator<int> ostream_it{ std::cout, " " };
1272+
const auto is_even = [](const auto n) { return n % 2 == 0; };
1273+
std::vector<int> v{ 0, 1, 2, 3, 4 };
1274+
1275+
// Print all even numbers.
1276+
std::copy_if(std::cbegin(v), std::cend(v), ostream_it, is_even); // 0 2 4
1277+
// Print all odd (not even) numbers.
1278+
std::copy_if(std::cbegin(v), std::cend(v), ostream_it, std::not_fn(is_even)); // 1 3
1279+
```
1280+
12671281
## C++14 Language Features
12681282
12691283
### Binary literals

0 commit comments

Comments
 (0)