Fix sloppy find_not() and find_*backward() code examples.

This commit is contained in:
Zach Laine
2018-10-02 17:22:49 -05:00
parent e4dfe0885f
commit 435cc61af8
2 changed files with 4 additions and 4 deletions

View File

@ -28,7 +28,7 @@ the use of `std::find()`:
auto rfirst = std::make_reverse_iterator(last); auto rfirst = std::make_reverse_iterator(last);
auto rlast = std::make_reverse_iterator(first); auto rlast = std::make_reverse_iterator(first);
auto it = std::find(rfirst, rlast); auto it = std::find(rfirst, rlast, x);
// Use it here... // Use it here...
That seems nicer in that there is no raw loop, but it has two major drawbacks. That seems nicer in that there is no raw loop, but it has two major drawbacks.

View File

@ -15,19 +15,19 @@ equal to the given value.
Consider this use of `find()`: Consider this use of `find()`:
auto std::vector<int> vec = { 1, 1, 2 }; std::vector<int> vec = { 1, 1, 2 };
auto it = std::find(vec.begin(), vec.end(), 1); auto it = std::find(vec.begin(), vec.end(), 1);
This gives us the first occurance of `1` in `vec`. What if we want to find This gives us the first occurance of `1` in `vec`. What if we want to find
the first occurrance of any number besides `1` in `vec`? We have to write an the first occurrance of any number besides `1` in `vec`? We have to write an
unfortunate amount of code: unfortunate amount of code:
auto std::vector<int> vec = { 1, 1, 2 }; std::vector<int> vec = { 1, 1, 2 };
auto it = std::find_if(vec.begin(), vec.end(), [](int i) { return i != 1; }); auto it = std::find_if(vec.begin(), vec.end(), [](int i) { return i != 1; });
With `find_not()` the code gets much more terse: With `find_not()` the code gets much more terse:
auto std::vector<int> vec = { 1, 1, 2 }; std::vector<int> vec = { 1, 1, 2 };
auto it = find_not(vec.begin(), vec.end(), 1); auto it = find_not(vec.begin(), vec.end(), 1);
The existing `find` variants are: `find()`, `find_if()`, and `find_if_not()`. The existing `find` variants are: `find()`, `find_if()`, and `find_if_not()`.