tmp/tmpz88jr3cd/{from.md → to.md}
RENAMED
|
@@ -30,52 +30,56 @@ iterator_traits<Iterator>::reference
|
|
| 30 |
iterator_traits<Iterator>::pointer
|
| 31 |
```
|
| 32 |
|
| 33 |
may be defined as `void`.
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
``` cpp
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
typedef typename Iterator::reference reference;
|
| 44 |
-
typedef typename Iterator::iterator_category iterator_category;
|
| 45 |
-
};
|
| 46 |
-
}
|
| 47 |
```
|
| 48 |
|
|
|
|
|
|
|
|
|
|
| 49 |
It is specialized for pointers as
|
| 50 |
|
| 51 |
``` cpp
|
| 52 |
namespace std {
|
| 53 |
template<class T> struct iterator_traits<T*> {
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
};
|
| 60 |
}
|
| 61 |
```
|
| 62 |
|
| 63 |
and for pointers to const as
|
| 64 |
|
| 65 |
``` cpp
|
| 66 |
namespace std {
|
| 67 |
template<class T> struct iterator_traits<const T*> {
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
};
|
| 74 |
}
|
| 75 |
```
|
| 76 |
|
|
|
|
|
|
|
| 77 |
To implement a generic `reverse` function, a C++program can do the
|
| 78 |
following:
|
| 79 |
|
| 80 |
``` cpp
|
| 81 |
template <class BidirectionalIterator>
|
|
@@ -91,5 +95,7 @@ void reverse(BidirectionalIterator first, BidirectionalIterator last) {
|
|
| 91 |
n -= 2;
|
| 92 |
}
|
| 93 |
}
|
| 94 |
```
|
| 95 |
|
|
|
|
|
|
|
|
|
| 30 |
iterator_traits<Iterator>::pointer
|
| 31 |
```
|
| 32 |
|
| 33 |
may be defined as `void`.
|
| 34 |
|
| 35 |
+
If `Iterator` has valid ([[temp.deduct]]) member types
|
| 36 |
+
`difference_type`, `value_type`, `pointer`, `reference`, and
|
| 37 |
+
`iterator_category`, `iterator_traits<Iterator>` shall have the
|
| 38 |
+
following as publicly accessible members:
|
| 39 |
|
| 40 |
``` cpp
|
| 41 |
+
using difference_type = typename Iterator::difference_type;
|
| 42 |
+
using value_type = typename Iterator::value_type;
|
| 43 |
+
using pointer = typename Iterator::pointer;
|
| 44 |
+
using reference = typename Iterator::reference;
|
| 45 |
+
using iterator_category = typename Iterator::iterator_category;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
```
|
| 47 |
|
| 48 |
+
Otherwise, `iterator_traits<Iterator>` shall have no members by any of
|
| 49 |
+
the above names.
|
| 50 |
+
|
| 51 |
It is specialized for pointers as
|
| 52 |
|
| 53 |
``` cpp
|
| 54 |
namespace std {
|
| 55 |
template<class T> struct iterator_traits<T*> {
|
| 56 |
+
using difference_type = ptrdiff_t;
|
| 57 |
+
using value_type = T;
|
| 58 |
+
using pointer = T*;
|
| 59 |
+
using reference = T&;
|
| 60 |
+
using iterator_category = random_access_iterator_tag;
|
| 61 |
};
|
| 62 |
}
|
| 63 |
```
|
| 64 |
|
| 65 |
and for pointers to const as
|
| 66 |
|
| 67 |
``` cpp
|
| 68 |
namespace std {
|
| 69 |
template<class T> struct iterator_traits<const T*> {
|
| 70 |
+
using difference_type = ptrdiff_t;
|
| 71 |
+
using value_type = T;
|
| 72 |
+
using pointer = const T*;
|
| 73 |
+
using reference = const T&;
|
| 74 |
+
using iterator_category = random_access_iterator_tag;
|
| 75 |
};
|
| 76 |
}
|
| 77 |
```
|
| 78 |
|
| 79 |
+
[*Example 1*:
|
| 80 |
+
|
| 81 |
To implement a generic `reverse` function, a C++program can do the
|
| 82 |
following:
|
| 83 |
|
| 84 |
``` cpp
|
| 85 |
template <class BidirectionalIterator>
|
|
|
|
| 95 |
n -= 2;
|
| 96 |
}
|
| 97 |
}
|
| 98 |
```
|
| 99 |
|
| 100 |
+
— *end example*]
|
| 101 |
+
|