STL中迭代器(Iterator)的簡單實(shí)現(xiàn)

 

成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站與策劃設(shè)計(jì),官渡網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:官渡等地區(qū)。官渡做網(wǎng)站價(jià)格咨詢:13518219792

 

#pragma once
struct InputIteratorTag {};
struct OutputIteratorTag {};
struct ForwardIteratorTag : public InputIteratorTag {};
struct BidirectionalIteratorTag : public ForwardIteratorTag {};
struct RandomAccessIteratorTag : public BidirectionalIteratorTag {};
template<class T> 
struct InputIterator
{
 //typedef T                  ValueType;
 //typedef Distance           DifferenceType;
 //typedef T*                 Pointer;
 //typedef T&                 Reference;
 typedef InputIteratorTag IteratorCategory; //迭代器的類型 ( 包括上面那5個(gè)struct )
};
template<class Iterator>
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<Iterator>::IteratorCategory IteratorCategory;
};
template<class T>
struct IteratorTraits<T*>  //內(nèi)置類型的特化版本
{
 typedef RandomAccessIteratorTag IteratorCategory;
};
template <class InputIterator>
inline size_t Distance(InputIterator first, InputIterator last)
{
 return _Distance(first, last, IteratorTraits<InputIterator>::IteratorCategory() );  //這里第三個(gè)參數(shù) 并沒有實(shí)際意義,不過正是它的存在,讓_Distance實(shí)現(xiàn)重載
}
template <class InputIterator>
inline size_t _Distance(InputIterator first, InputIterator last, RandomAccessIteratorTag)
{
 return last - first;
}
template <class InputIterator>
inline size_t _Distance(InputIterator first, InputIterator last, InputIteratorTag)
{
 size_t n = 0;
 while (first != last)
 {
  ++first;
  ++n;
 }
 return n;
}

 

當(dāng)前標(biāo)題:STL中迭代器(Iterator)的簡單實(shí)現(xiàn)
網(wǎng)站地址:http://bm7419.com/article10/pcohgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、網(wǎng)頁設(shè)計(jì)公司、品牌網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)