STL中迭代器(Iterator)的简单实现
成都创新互联是一家专注于成都网站设计、成都做网站与策划设计,官渡网站建设哪家好?成都创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:官渡等地区。官渡做网站价格咨询:13518219792
#pragma once struct InputIteratorTag {}; struct OutputIteratorTag {}; struct ForwardIteratorTag : public InputIteratorTag {}; struct BidirectionalIteratorTag : public ForwardIteratorTag {}; struct RandomAccessIteratorTag : public BidirectionalIteratorTag {}; templatestruct InputIterator { //typedef T ValueType; //typedef Distance DifferenceType; //typedef T* Pointer; //typedef T& Reference; typedef InputIteratorTag IteratorCategory; //迭代器的类型 ( 包括上面那5个struct ) }; template struct IteratorTraits { //typedef typename Iterator::ValueType ValueType; //typedef typename Iterator::DifferenceType DifferenceType; //typedef typename Iterator::Pointer Pointer; //typedef typename Iterator::Reference Reference; typedef typename InputIterator ::IteratorCategory IteratorCategory; }; template struct IteratorTraits //内置类型的特化版本 { typedef RandomAccessIteratorTag IteratorCategory; }; template inline size_t Distance(InputIterator first, InputIterator last) { return _Distance(first, last, IteratorTraits ::IteratorCategory() ); //这里第三个参数 并没有实际意义,不过正是它的存在,让_Distance实现重载 } template inline size_t _Distance(InputIterator first, InputIterator last, RandomAccessIteratorTag) { return last - first; } template inline size_t _Distance(InputIterator first, InputIterator last, InputIteratorTag) { size_t n = 0; while (first != last) { ++first; ++n; } return n; }
网站栏目:STL中迭代器(Iterator)的简单实现
文章起源:http://azwzsj.com/article/pcohgo.html