CARVIEW |
Select Language
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 23 Jul 2025 02:03:30 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: /reference/iterator/iterator_traits/
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 23 Jul 2025 02:03:30 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"7920-7NHxe6PmYwRnMmYA4JGL1HK0ARE"
Content-Encoding: gzip
Traits class defining properties of iterators.
Standard algorithms determine certain properties of the iterators passed to them and the range they represent by using the members of the corresponding iterator_traits instantiation.
For every iterator type, a corresponding specialization of iterator_traits class template shall be defined, with at least the following member types defined:
Note: For output iterators that are not at least forward iterators, any of these member types (except for iterator_category) may be defined as
The iterator_traits class template comes with a default definition that obtains these types from the iterator type itself (see below). It is also specialized for pointers (T*) and pointers to const (const T*).
Note that any custom class will have a valid instantiation of iterator_traits if it publicly inherits the base class std::iterator.
Output:
Reference
C library:
- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
-
<cfenv> (fenv.h)C++11
- <cfloat> (float.h)
-
<cinttypes> (inttypes.h)C++11
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
-
<cstdbool> (stdbool.h)C++11
- <cstddef> (stddef.h)
-
<cstdint> (stdint.h)C++11
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
-
<ctgmath> (tgmath.h)C++11
- <ctime> (time.h)
-
<cuchar> (uchar.h)C++11
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
-
<array>C++11
- <deque>
-
<forward_list>C++11
- <list>
- <map>
- <queue>
- <set>
- <stack>
-
<unordered_map>C++11
-
<unordered_set>C++11
- <vector>
-
Input/Output:
Multi-threading:
-
<atomic>C++11
-
<condition_variable>C++11
-
<future>C++11
-
<mutex>C++11
-
<thread>C++11
-
Other:
- <algorithm>
- <bitset>
-
<chrono>C++11
-
<codecvt>C++11
- <complex>
- <exception>
- <functional>
-
<initializer_list>C++11
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
-
<random>C++11
-
<ratio>C++11
-
<regex>C++11
- <stdexcept>
- <string>
-
<system_error>C++11
-
<tuple>C++11
-
<type_traits>C++11
-
<typeindex>C++11
- <typeinfo>
- <utility>
- <valarray>
<iterator>
- iterator
- iterator_traits
functions
- advance
- back_inserter
-
beginC++11
- distance
-
endC++11
- front_inserter
- inserter
-
make_move_iteratorC++11
-
nextC++11
-
prevC++11
iterator categories
predefined iterators
- Reference
- <iterator>
- iterator_traits
class template
<iterator>
std::iterator_traits
template <class Iterator> class iterator_traits;template <class T> class iterator_traits<T*>;template <class T> class iterator_traits<const T*>;
Iterator traits
Standard algorithms determine certain properties of the iterators passed to them and the range they represent by using the members of the corresponding iterator_traits instantiation.
For every iterator type, a corresponding specialization of iterator_traits class template shall be defined, with at least the following member types defined:
member | description |
---|---|
difference_type | Type to express the result of subtracting one iterator from another. |
value_type | The type of the element the iterator can point to. |
pointer | The type of a pointer to an element the iterator can point to. |
reference | The type of a reference to an element the iterator can point to. |
iterator_category | The iterator category. It can be one of these: |
Note: For output iterators that are not at least forward iterators, any of these member types (except for iterator_category) may be defined as
void
.The iterator_traits class template comes with a default definition that obtains these types from the iterator type itself (see below). It is also specialized for pointers (T*) and pointers to const (const T*).
Note that any custom class will have a valid instantiation of iterator_traits if it publicly inherits the base class std::iterator.
Member types
member | generic definition | T* specialization | const T* specialization |
---|---|---|---|
difference_type | Iterator::difference_type | ptrdiff_t | ptrdiff_t |
value_type | Iterator::value_type | T | T |
pointer | Iterator::pointer | T* | const T* |
reference | Iterator::reference | T& | const T& |
iterator_category | Iterator::iterator_category | random_access_iterator_tag | random_access_iterator_tag |
Example
|
|
Output:
int* is a random-access iterator |
See also
- iterator
- Iterator base class (class template)
Home page | Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us