std::iterator_traits<std::common_iterator>
来自cppreference.com
< cpp | iterator | common iterator
在标头 <iterator> 定义
|
||
template< std::input_iterator I, class S > struct iterator_traits<std::common_iterator<I, S>>; |
(C++20 起) | |
提供对于 std::common_iterator 类型的属性的统一接口。
成员类型
成员类型 | 定义 |
iterator_concept
|
std::forward_iterator_tag ,若 I 实现 std::forward_iterator ; 否则为 std::input_iterator_tag |
iterator_category
|
std::forward_iterator_tag ,若 std::iterator_traits<I>::iterator_category 合法并指代实现 std::derived_from<std::forward_iterator_tag> 的类型; 否则为 std::input_iterator_tag |
value_type
|
std::iter_value_t<I> |
difference_type
|
std::iter_difference_t<I> |
pointer
|
decltype(c.operator->()) ,若它为良构,其中 c 为 const std::common_iterator<I, S> 类型左值; 否则为 void |
reference
|
std::iter_reference_t<I> |
示例
运行此代码
#include <vector> #include <iterator> #include <type_traits> int main() { std::vector v{1,2,3,4}; using CV = std::common_iterator< std::counted_iterator<std::vector<int>::iterator>, std::default_sentinel_t>; CV i { std::counted_iterator { v.begin(), 3 } }; using TRCV = std::iterator_traits<decltype(i)>; static_assert(std::is_same<TRCV::iterator_concept, std::forward_iterator_tag>()); static_assert(std::is_same<TRCV::iterator_category, std::forward_iterator_tag>()); static_assert(std::is_same<TRCV::value_type, int>()); static_assert(std::is_same<TRCV::difference_type, std::vector<int>::difference_type>()); static_assert(std::is_same<TRCV::pointer, decltype(i.operator->())>()); static_assert(std::is_same<TRCV::reference, int&>()); CV s { std::default_sentinel }; using TRCS = std::iterator_traits<decltype(s)>; static_assert(std::is_same<TRCS::iterator_concept, std::forward_iterator_tag>()); static_assert(std::is_same<TRCS::iterator_category, std::forward_iterator_tag>()); static_assert(std::is_same<TRCS::value_type, int>()); static_assert(std::is_same<TRCS::difference_type, std::vector<int>::difference_type>()); static_assert(std::is_same<TRCS::pointer, decltype(s.operator->())>()); static_assert(std::is_same<TRCS::reference, int&>()); }
参阅
为迭代器各项性质提供统一接口 (类模板) | |
计算 std::common_iterator 类型的关联差类型 (类模板特化) |