find_not.qbk 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. [/ File find_not.qbk]
  2. [section:find_not find_not ]
  3. [/license
  4. Copyright (c) 2018 T. Zachary Laine
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ]
  8. The header file 'find_not.hpp' contains a variants of a the stl algorithm
  9. `find`. The algorithm finds the first value in the given sequence that is not
  10. equal to the given value.
  11. Consider this use of `find()`:
  12. std::vector<int> vec = { 1, 1, 2 };
  13. auto it = std::find(vec.begin(), vec.end(), 1);
  14. This gives us the first occurance of `1` in `vec`. What if we want to find
  15. the first occurrance of any number besides `1` in `vec`? We have to write an
  16. unfortunate amount of code:
  17. std::vector<int> vec = { 1, 1, 2 };
  18. auto it = std::find_if(vec.begin(), vec.end(), [](int i) { return i != 1; });
  19. With `find_not()` the code gets much more terse:
  20. std::vector<int> vec = { 1, 1, 2 };
  21. auto it = find_not(vec.begin(), vec.end(), 1);
  22. The existing `find` variants are: `find()`, `find_if()`, and `find_if_not()`.
  23. It seems natural to also have `find_not()`, for the very reason that we have
  24. `find_if_not()` -- to avoid having to write a lambda to wrap the negation of
  25. the find condition.
  26. [heading interface]
  27. template<typename InputIter, typename Sentinel, typename T>
  28. InputIter find_not(InputIter first, Sentinel last, const T & x);
  29. template<typename Range, typename T>
  30. boost::range_iterator<Range> find_not(Range & r, const T & x);
  31. These overloads of `find_not` return the first value that is not equal to `x`
  32. in the sequence `[first, last)` or `r`, respectively.
  33. [heading Examples]
  34. Given the container `c1` containing `{ 0, 1, 2 }`, then
  35. find_not ( c1.begin(), c1.end(), 1 ) --> c1.begin()
  36. find_not ( c1.begin(), c1.end(), 0 ) --> std::next(c1.begin())
  37. [heading Iterator Requirements]
  38. `find_not` works on all iterators except output iterators.
  39. The template parameter `Sentinel` is allowed to be different from `InputIter`,
  40. or they may be the same. For an `InputIter` `it` and a `Sentinel` `end`, `it
  41. == end` and `it != end` must be well-formed expressions.
  42. [heading Complexity]
  43. Linear.
  44. [heading Exception Safety]
  45. `find_not` takes its parameters by value and do not depend upon any global
  46. state. Therefore, it provides the strong exception guarantee.
  47. [heading Notes]
  48. `constexpr` in C++14 or later.
  49. [endsect]
  50. [/ File equal.qbk
  51. Copyright 2018 T. Zachary Laine
  52. Distributed under the Boost Software License, Version 1.0.
  53. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
  54. ]