iterator 的一些特殊用法
本文章基于 VS2019 创作。
std::iterator 这是一个基类模板,可用于从其派生迭代器类。它不是迭代器类,并且不提供迭代器应具有的任何功能。
该基类仅提供了一些构件类型,这实际上是不存在于任何迭代器类型(迭代器类型没有具体的构件的要求)所需的,但由于它们限定所需默认成员,他们可能是有用的,iterator_traits 类模板自动生成适当的实例(并且这样的实例化是必需适用于所有的迭代器类型)。
std::iterator 被定义为:
template <class Category, class T, class Distance = ptrdiff_t, class Pointer = T*, class Reference = T&>
struct iterator {
typedef T value_type; //元素的类型
typedef Distance difference_type; //两个迭代器之间的距离
typedef Pointer pointer; //元素的指针
typedef Reference reference; //元素的引用
typedef Category iterator_category;//迭代器类型
};
itertor 在 vector const iterator 中的实际运用:
template <class _Myvec>
class _Vector_const_iterator : public _Iterator_base {
public:
#ifdef __cpp_lib_concepts
using iterator_concept = contiguous_iterator_tag;
#endif // __cpp_lib_concepts
using iterator_category = random_access_iterator_tag;
using value_type = typename _Myvec::value_type;
using difference_type = typename _Myvec::difference_type;
using pointer = typename _Myvec