algorithms.qbk 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. [section:algorithms Algorithms]
  2. [section:advance Function template `advance()`]
  3. The `boost::iterators::advance` function template is an adapted version of `std::advance` for the Boost iterator [link iterator.concepts.traversal traversal concepts].
  4. [heading Header]
  5. <boost/iterator/advance.hpp>
  6. [heading Synopsis]
  7. template <typename Iterator, typename Distance>
  8. constexpr void advance(Iterator& it, Distance n);
  9. [heading Description]
  10. Moves `it` forward by `n` increments (or backward by `|n|` decrements if `n` is negative).
  11. [heading Requirements]
  12. `Iterator` should model Incrementable Iterator.
  13. [heading Preconditions]
  14. Let `it`[sub `i`] be the iterator obtained by incrementing (or decrementing if `n` is negative) `it` by `i`. All the iterators `it`[sub `i`] for `i` = 0, 1, 2, ..., `|n|` should be valid.
  15. If `Iterator` does not model [link iterator.concepts.traversal.bidirectional Bidirectional Traversal Iterator], `n` should be non-negative.
  16. [heading Complexity]
  17. If `Iterator` models [link iterator.concepts.traversal.random_access Random Access Traversal Iterator], it takes constant time; otherwise it takes linear time.
  18. [heading Notes]
  19. * This function is not a customization point and is protected against being found by argument-dependent lookup (ADL).
  20. * This function is `constexpr` only in C++14 or later.
  21. [heading Acknowledgements]
  22. Contributed by Michel Morin.
  23. [endsect]
  24. [section:distance Function template `distance()`]
  25. The `boost::iterators::distance` function template is an adapted version of `std::distance` for the Boost iterator [link iterator.concepts.traversal traversal concepts].
  26. [heading Header]
  27. <boost/iterator/distance.hpp>
  28. [heading Synopsis]
  29. template <typename Iterator>
  30. constexpr typename iterator_difference<Iterator>::type
  31. distance(Iterator first, Iterator last);
  32. [heading Description]
  33. Computes the (signed) distance from `first` to `last`.
  34. [heading Requirements]
  35. `Iterator` should model [link iterator.concepts.traversal.single_pass Single Pass Iterator].
  36. [heading Preconditions]
  37. If `Iterator` models [link iterator.concepts.traversal.random_access Random Access Traversal Iterator], `[first, last)` or `[last, first)` should be valid; otherwise `[first, last)` should be valid.
  38. [heading Complexity]
  39. If `Iterator` models [link iterator.concepts.traversal.random_access Random Access Traversal Iterator], it takes constant time; otherwise it takes linear time.
  40. [heading Notes]
  41. * This function is not a customization point and is protected against being found by argument-dependent lookup (ADL).
  42. * This function is `constexpr` only in C++14 or later.
  43. [heading Acknowledgements]
  44. Contributed by Michel Morin.
  45. [endsect]
  46. [section:next_prior Function templates `next()` and `prior()`]
  47. Certain data types, such as the C++ Standard Library's forward and bidirectional iterators, do not provide addition and subtraction via `operator+()` or `operator-()`. This means that non-modifying computation of the next or prior value requires a temporary, even though `operator++()` or `operator--()` is provided. It also means that writing code like `itr+1` inside a template restricts the iterator category to random access iterators.
  48. The `next()` and `prior()` functions defined in `boost/next_prior.hpp` provide a simple way around these problems.
  49. [heading Synopsis]
  50. template <class T>
  51. T next(T x)
  52. {
  53. return ++x;
  54. }
  55. template <class T, class Distance>
  56. T next(T x, Distance n)
  57. {
  58. std::advance(x, n);
  59. return x;
  60. }
  61. template <class T>
  62. T prior(T x)
  63. {
  64. return --x;
  65. }
  66. template <class T, class Distance>
  67. T prior(T x, Distance n)
  68. {
  69. std::advance(x, -n);
  70. return x;
  71. }
  72. [note Function implementations above are given for exposition only. The actual implementation has the same effect for iterators, but has different properties, as documented later.]
  73. [heading Usage]
  74. Usage is simple:
  75. const std::list<T>::iterator p = get_some_iterator();
  76. const std::list<T>::iterator prev = boost::prior(p);
  77. const std::list<T>::iterator next = boost::next(prev, 2);
  78. The distance from the given iterator should be supplied as an absolute value. For example, the iterator four iterators prior to the given iterator `p` may be obtained by `prior(p, 4)`.
  79. With C++11, the Standard Library provides `std::next()` and `std::prev()` function templates, which serve the same purpose. However, there are advantages to `boost::next()` and `boost::prior()`.
  80. First, `boost::next()` and `boost::prior()` are compatible not only with iterators but with any type that provides arithmetic operators `operator++()`, `operator--()`, `operator+()`, `operator-()`, `operator+=()` or `operator-=()`. For example, this is possible:
  81. int x = 10;
  82. int y = boost::next(x, 5);
  83. assert(y == 15);
  84. Second, `boost::next()` and `boost::prior()` use [link iterator.concepts.traversal traversal categories] to select the most efficient implementation. For some kinds of iterators, such as [link iterator.specialized.transform transform iterators], the standard iterator category does not reflect the traversal category correctly and therefore `std::next()` and `std::prev()` will fall back to linear complexity.
  85. [heading Acknowledgements]
  86. Contributed by [@http://www.boost.org/people/dave_abrahams.htm Dave Abrahams]. Two-argument versions by Daniel Walker.
  87. [endsect]
  88. [endsect]