std::iterator_traits
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <iterator>
|
||
| template< class Iterator> struct iterator_traits; |
||
| template< class T > struct iterator_traits<T*>; |
||
| template< class T > struct iterator_traits<const T*>; |
||
std::iterator_traits的类型特征的类,它提供了统一的接口,迭代器类型的属性。这使得它可以实现算法的迭代器.Original:
std::iterator_traits is the type trait class that provides uniform interface to the properties of iterator types. This makes it possible to implement algorithms only in terms of iterators.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 会员类型
| 会员类型
Original: Member type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Definition |
difference_type
|
Iterator::difference_type
|
value_type
|
Iterator::value_type
|
pointer
|
Iterator::pointer
|
reference
|
Iterator::reference
|
iterator_category
|
Iterator::iterator_category
|
[编辑] 专业化
此类型的性状可以专门为用户提供的类型,也可以使用作为迭代器。标准库提供了两个偏特化类型的指针,这使得它可以使用所有的基于迭代器的算法与原始指针T *.
Original:
This type trait may be specialized for user-provided types that may be used as iterators. The standard library provides two partial specializations for pointer types T*, which makes it possible to use all iterator-based algorithms with raw pointers.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] T *专业化的成员类型
| 会员类型
Original: Member type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Definition |
difference_type
|
std::ptrdiff_t |
value_type
|
T
|
pointer
|
T*
|
reference
|
T&
|
iterator_category
|
std::random_access_iterator_tag |
[编辑] 常量T *专业化的成员类型
| 会员类型
Original: Member type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Definition |
difference_type
|
std::ptrdiff_t |
value_type
|
T
|
pointer
|
const T*
|
reference
|
const T&
|
iterator_category
|
std::random_access_iterator_tag |
[编辑] 为例
通用反向(双向迭代器)实现
Original:
general-purpose reverse() implementation for bidirectional iterators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#include <iostream> #include <iterator> #include <vector> #include <list> template<class BDIter> void my_reverse(BDIter first, BDIter last) { typename std::iterator_traits<BDIter>::difference_type n = std::distance(first, last); --n; while(n > 0) { typename std::iterator_traits<BDIter>::value_type tmp = *first; *first++ = *--last; *last = tmp; n -= 2; } } int main() { std::vector<int> v{1,2,3,4,5}; my_reverse(v.begin(), v.end()); for(int n : v) std::cout << n << ' '; std::cout << '\n'; std::list<int> l{1,2,3,4,5}; my_reverse(l.begin(), l.end()); for(auto n : l) std::cout << n << ' '; std::cout << '\n'; // std::istreambuf_iterator<char> i1(std::cin), i2; // my_reverse(i1, i2); // compilation error }
Output:
5 4 3 2 1 5 4 3 2 1
[编辑] 另请参阅
| 基本的迭代器 Original: the basic iterator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (类模板) | |
| 空类类型用来表示迭代器类别 Original: empty class types used to indicate iterator categories The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (类) | |