STL中迭代器(Iterator)的简单实现-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
STL中迭代器(Iterator)的简单实现

 

成都创新互联是一家专注于成都网站设计、成都做网站与策划设计,官渡网站建设哪家好?成都创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:官渡等地区。官渡做网站价格咨询:13518219792

 

#pragma once
struct InputIteratorTag {};
struct OutputIteratorTag {};
struct ForwardIteratorTag : public InputIteratorTag {};
struct BidirectionalIteratorTag : public ForwardIteratorTag {};
struct RandomAccessIteratorTag : public BidirectionalIteratorTag {};
template 
struct 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://scgulin.cn/article/pcohgo.html