1. 算法如何知道迭代器数据类型?
1
2
3
4
5
typedef bidirectional_iterator_tag iteraotr_category;
typedef T value_type;
typedef Ptr pointer;
typedef Ref reference;
typedef ptrdiff_t defference_type;
很简单 直接点即可
1
2
3
4
algorithm(I first,T last){
I::pointer
}
问题2 那要是传入的是指针而不是class呢?
1.
1
2
3
4
algorithm(I first,T last){
typename iterator_traits<T>::value_type vaue_type;
}
1 2 3 4
template<class T> struct iterator_traits{ typename iterator_traits<T>::value_type vaue_type; }
- 针对指针特化
1 2 3 4
template<class T> struct iterator_traits<T*>{ typename T vaue_type; }
- 针对const 特化
1 2 3 4
template<class T> struct iterator_traits<const T*>{ typename T vaue_type; // 不可返回const T 变量无法赋值 }