iterator.h.xml 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <chapter xml:id="iterator.h">
  2. <title><tt>__vic/iterator.h</tt></title>
  3. <p>Iterators-related utilities.</p>
  4. <chapter xml:id="begin-array">
  5. <title><tt>begin(T[])</tt>, <tt>end(T[])</tt>, <tt>cbegin(T[])</tt>,
  6. <tt>cend(T[])</tt></title>
  7. <code-block lang="C++"><![CDATA[
  8. template<class T, size_t N> constexpr T *begin(T (&arr)[N]);
  9. template<class T, size_t N> constexpr T *end(T (&arr)[N]);
  10. template<class T, size_t N> constexpr const T *cbegin(T (&arr)[N]);
  11. template<class T, size_t N> constexpr const T *cend(T (&arr)[N]);
  12. ]]></code-block>
  13. <p>Return a pointer to the beginning and past-the-end of the array.</p>
  14. <section><title>Example</title>
  15. <code-block lang="C++"><![CDATA[
  16. int vals[] = { 1, 2, 3 };
  17. std::list<int> lst(__vic::begin(vals), __vic::end(vals));
  18. assert(lst.size() == 3);
  19. ]]></code-block>
  20. </section>
  21. </chapter>
  22. <chapter xml:id="advance">
  23. <title><tt>advance()</tt></title>
  24. <code-block lang="C++"><![CDATA[
  25. template<class Iter>
  26. void advance(Iter &it, Iter end, size_t n);
  27. ]]></code-block>
  28. <p>A counterpart of <tt>std::advance()</tt> but differs in parameters and
  29. behaviour:</p>
  30. <list style="numbered">
  31. <item>Allows only forward movement (<tt>++</tt>),</item>
  32. <item>Checks the range bounds. Returns immediately if <tt>end</tt> is
  33. reached.</item>
  34. </list>
  35. </chapter>
  36. <chapter xml:id="next">
  37. <title><tt>next()</tt>, <tt>prev()</tt></title>
  38. <code-block lang="C++"><![CDATA[
  39. template<class ForwardIterator>
  40. ForwardIterator next(ForwardIterator it);
  41. template<class ForwardIterator>
  42. ForwardIterator next(ForwardIterator it, size_t n);
  43. template<class BidirectionalIterator>
  44. BidirectionalIterator prev(BidirectionalIterator it);
  45. template<class BidirectionalIterator>
  46. BidirectionalIterator prev(BidirectionalIterator it, size_t n);
  47. ]]></code-block>
  48. <p><tt>next()</tt> returns an iterator advanced by <tt>n</tt> positions.
  49. <tt>prev()</tt> does the same but in reverse order. As opposed to C++11 STL
  50. functions of the same name, the offset cannot be negative. The versions with
  51. single parameter just call <tt>++it</tt>/<tt>--it</tt> and return the result.
  52. </p>
  53. <section><title>Example</title>
  54. <code-block lang="C++"><![CDATA[
  55. template<class Container>
  56. void f(const Container &c)
  57. {
  58. // Begin a traversal starting from the second element
  59. // v.begin() + 1 works only with RandomAccessIterator
  60. // ++v.begin() may cause a compile error
  61. for(auto it = __vic::next(c.begin()); it != c.end(); ++it) ...;
  62. }
  63. ]]></code-block>
  64. </section>
  65. </chapter>
  66. </chapter>