one_of.qbk 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. [/ File one_of.qbk]
  2. [section:one_of one_of]
  3. [/license
  4. Copyright (c) 2010-2012 Marshall Clow
  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 'boost/algorithm/cxx11/one_of.hpp' contains four variants of a single algorithm, `one_of`. The algorithm tests the elements of a sequence and returns true if exactly one of the elements in the sequence has a particular property.
  9. The routine `one_of` takes a sequence and a predicate. It will return true if the predicate returns true for one element in the sequence.
  10. The routine `one_of_equal` takes a sequence and a value. It will return true if one element in the sequence compares equal to the passed in value.
  11. Both routines come in two forms; the first one takes two iterators to define the range. The second form takes a single range parameter, and uses Boost.Range to traverse it.
  12. [heading interface]
  13. The function `one_of` returns true if the predicate returns true for one item in the sequence. There are two versions; one takes two iterators, and the other takes a range.
  14. ``
  15. namespace boost { namespace algorithm {
  16. template<typename InputIterator, typename Predicate>
  17. bool one_of ( InputIterator first, InputIterator last, Predicate p );
  18. template<typename Range, typename Predicate>
  19. bool one_of ( const Range &r, Predicate p );
  20. }}
  21. ``
  22. The function `one_of_equal` is similar to `one_of`, but instead of taking a predicate to test the elements of the sequence, it takes a value to compare against.
  23. ``
  24. namespace boost { namespace algorithm {
  25. template<typename InputIterator, typename V>
  26. bool one_of_equal ( InputIterator first, InputIterator last, V const &val );
  27. template<typename Range, typename V>
  28. bool one_of_equal ( const Range &r, V const &val );
  29. }}
  30. ``
  31. [heading Examples]
  32. Given the container `c` containing `{ 0, 1, 2, 3, 14, 15 }`, then
  33. ``
  34. bool isOdd ( int i ) { return i % 2 == 1; }
  35. bool lessThan10 ( int i ) { return i < 10; }
  36. using boost::algorithm;
  37. one_of ( c, isOdd ) --> false
  38. one_of ( c.begin (), c.end (), lessThan10 ) --> false
  39. one_of ( c.begin () + 3, c.end (), lessThan10 ) --> true
  40. one_of ( c.end (), c.end (), isOdd ) --> false // empty range
  41. one_of_equal ( c, 3 ) --> true
  42. one_of_equal ( c.begin (), c.begin () + 3, 3 ) --> false
  43. one_of_equal ( c.begin (), c.begin (), 99 ) --> false // empty range
  44. ``
  45. [heading Iterator Requirements]
  46. `one_of` and `one_of_equal` work on all iterators except output iterators.
  47. [heading Complexity]
  48. All of the variants of `one_of` and `one_of_equal` run in ['O(N)] (linear) time; that is, they compare against each element in the list once. If more than one of the elements in the sequence satisfy the condition, then algorithm will return false immediately, without examining the remaining members of the sequence.
  49. [heading Exception Safety]
  50. All of the variants of `one_of` and `one_of_equal` take their parameters by value or const reference, and do not depend upon any global state. Therefore, all the routines in this file provide the strong exception guarantee.
  51. [heading Notes]
  52. * `one_of` and `one_of_equal` both return false for empty ranges, no matter what is passed to test against.
  53. * The second parameter to `one_of_equal` is a template parameter, rather than deduced from the first parameter (`std::iterator_traits<InputIterator>::value_type`) because that allows more flexibility for callers, and takes advantage of built-in comparisons for the type that is pointed to by the iterator. The function is defined to return true if, for one element in the sequence, the expression `*iter == val` evaluates to true (where `iter` is an iterator to each element in the sequence)
  54. [endsect]
  55. [/ File one_of.qbk
  56. Copyright 2011 Marshall Clow
  57. Distributed under the Boost Software License, Version 1.0.
  58. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
  59. ]