ordered-hpp.qbk 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. [/ QuickBook Document version 1.5 ]
  2. [section:is_sorted is_sorted ]
  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
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. ]
  9. The header file `<boost/algorithm/cxx11/is_sorted.hpp>` contains functions for determining if a sequence is ordered.
  10. [heading is_sorted]
  11. The function `is_sorted(sequence)` determines whether or not a sequence is completely sorted according so some criteria. If no comparison predicate is specified, then `std::less` is used (i.e, the test is to see if the sequence is non-decreasing)
  12. ``
  13. namespace boost { namespace algorithm {
  14. template <typename ForwardIterator, typename Pred>
  15. bool is_sorted ( ForwardIterator first, ForwardIterator last, Pred p );
  16. template <typename ForwardIterator>
  17. bool is_sorted ( ForwardIterator first, ForwardIterator last );
  18. template <typename Range, typename Pred>
  19. bool is_sorted ( const Range &r, Pred p );
  20. template <typename Range>
  21. bool is_sorted ( const Range &r );
  22. }}
  23. ``
  24. Iterator requirements: The `is_sorted` functions will work forward iterators or better.
  25. [heading is_sorted_until]
  26. If `distance(first, last) < 2`, then `is_sorted ( first, last )` returns `last`. Otherwise, it returns the last iterator i in [first,last] for which the range [first,i) is sorted.
  27. In short, it returns the element in the sequence that is "out of order". If the entire sequence is sorted (according to the predicate), then it will return `last`.
  28. ``
  29. namespace boost { namespace algorithm {
  30. template <typename ForwardIterator, typename Pred>
  31. FI is_sorted_until ( ForwardIterator first, ForwardIterator last, Pred p );
  32. template <typename ForwardIterator>
  33. ForwardIterator is_sorted_until ( ForwardIterator first, ForwardIterator last );
  34. template <typename Range, typename Pred>
  35. typename boost::range_iterator<const R>::type is_sorted_until ( const Range &r, Pred p );
  36. template <typename Range>
  37. typename boost::range_iterator<const R>::type is_sorted_until ( const Range &r );
  38. }}
  39. ``
  40. Iterator requirements: The `is_sorted_until` functions will work on forward iterators or better. Since they have to return a place in the input sequence, input iterators will not suffice.
  41. Complexity:
  42. `is_sorted_until` will make at most ['N-1] calls to the predicate (given a sequence of length ['N]).
  43. Examples:
  44. Given the sequence `{ 1, 2, 3, 4, 5, 3 }`, `is_sorted_until ( beg, end, std::less<int>())` would return an iterator pointing at the second `3`.
  45. Given the sequence `{ 1, 2, 3, 4, 5, 9 }`, `is_sorted_until ( beg, end, std::less<int>())` would return `end`.
  46. There are also a set of "wrapper functions" for is_ordered which make it easy to see if an entire sequence is ordered. These functions return a boolean indicating success or failure rather than an iterator to where the out of order items were found.
  47. To test if a sequence is increasing (each element at least as large as the preceding one):
  48. ``
  49. namespace boost { namespace algorithm {
  50. template <typename Iterator>
  51. bool is_increasing ( Iterator first, Iterator last );
  52. template <typename R>
  53. bool is_increasing ( const R &range );
  54. }}
  55. ``
  56. To test if a sequence is decreasing (each element no larger than the preceding one):
  57. ``
  58. namespace boost { namespace algorithm {
  59. template <typename ForwardIterator>
  60. bool is_decreasing ( ForwardIterator first, ForwardIterator last );
  61. template <typename R>
  62. bool is_decreasing ( const R &range );
  63. }}
  64. ``
  65. To test if a sequence is strictly increasing (each element larger than the preceding one):
  66. ``
  67. namespace boost { namespace algorithm {
  68. template <typename ForwardIterator>
  69. bool is_strictly_increasing ( ForwardIterator first, ForwardIterator last );
  70. template <typename R>
  71. bool is_strictly_increasing ( const R &range );
  72. }}
  73. ``
  74. To test if a sequence is strictly decreasing (each element smaller than the preceding one):
  75. ``
  76. namespace boost { namespace algorithm {
  77. template <typename ForwardIterator>
  78. bool is_strictly_decreasing ( ForwardIterator first, ForwardIterator last );
  79. template <typename R>
  80. bool is_strictly_decreasing ( const R &range );
  81. }}
  82. ``
  83. Complexity:
  84. Each of these calls is just a thin wrapper over `is_sorted`, so they have the same complexity as `is_sorted`.
  85. [heading Notes]
  86. * The routines `is_sorted` and `is_sorted_until` are part of the C++11 standard. When compiled using a C++11 implementation, the implementation from the standard library will be used.
  87. * `is_sorted` and `is_sorted_until` both return true for empty ranges and ranges of length one.
  88. [endsect]