在接觸了STL後,覺得STL就是一個爲效率而生的怪物,而且令人驚訝的是它裏面的很多算法和模板對於學C++的人來說是非常有價值去剖析的。
迭代器(Iterator)模式--提供一種方法,使之能夠依序尋訪某個聚合物(容器)所含的各個元素,而又無需暴露該聚合物的內部表達方式。
STL的中心思想在於:將數據容器和算法分開,彼此獨立設計,最後再以一貼膠合劑(iterator)將它們撮合在一起。
迭代器是數據結構容器中非常有用的“特殊指針”,在不暴露容器中結構的條件下,可以取出容器裏所有的元素。
<span style="font-family:Microsoft YaHei;font-size:14px;">vector<int > v1;
v1.push_back (5);
v1.push_back (4);
v1.push_back (3);
v1.push_back (2);
v1.push_back (1);
// 迭代器遍歷順序表
vector<int >::iterator it = v1 .begin();
for (; it != v1. end(); ++it )
{
cout<<*it <<" ";
}
cout<<endl ;</span>
相信很多人在看STL源碼的時候會有很多的疑問,確實STL是大神們寫的代碼。我們很難去看懂。裏面的宏定義和輔助函數我們可以忽略,只把整個框架提取出來,再往框架中不斷的添加功能,最後就會成爲STL源碼。所以我們現在的任務就是提取Iterator的框架。
一:Iterator的調用接口:
<span style="font-family:Microsoft YaHei;font-size:14px;">//通過Distance來計算兩個迭代器之間的距離
//通過Category來確定是前向迭代器還是雙向迭代器,還是隨機迭代器
template<class T, class Distance, class Category,
class Pointer = T*, class Reference = T&>
</span><pre name="code" class="cpp"><span style="font-family:Microsoft YaHei;">struct Iterator
{
typedef Category CategoryIterator;//迭代器類型
typedef T ValueType; //迭代器所指對象的類型
typedef Distance DifferenceType; //兩個迭代器之間的距離
typedef Pointer Pointer; //迭代器所指類型的指針
typedef Reference Reference; //迭代器所指類型的引用
};</span>
這是Iterator的入口,我們可以看到class Iterator中其實沒有很多的東西,就是有5個typedef。利用模板傳進來的參數CategoryIterator可以提取出迭代器的類型:
二:迭代器的類型:
<span style="font-family:Microsoft YaHei;font-size:14px;">struct InputIteratorTag{};
struct OutputIteratorTag{};
struct ForwardIteratorTag :<span style="color:#ff0000;">public </span>InputIteratorTag{};
struct BidirectionalIteratorTag :<span style="color:#ff0000;">public</span> InputIteratorTag{};
struct RandomAccessIteratorTag :<span style="color:#ff0000;">public </span>InputIteratorTag{};
//Distance是迭代器之間的距離
template<class T,class Distance>
struct InputIterator //只讀迭代器
{
typedef InputIteratorTag CategoryIterator;
typedef T ValueType;
typedef Distance DifferenceType;
typedef T* Pointer;
typedef T& Reference;
};
</span><pre name="code" class="cpp"><span style="font-family:Microsoft YaHei;font-size:14px;">template<class T, class Distance>
struct OutputIterator //只寫迭代器
{
typedef OutputIteratorTag CategoryIterator;
typedef T ValueType;
typedef Distance DifferenceType;
typedef T* Pointer;
typedef T& Reference;
};</span>
template<class T, class Distance>struct ForwardIterator //前向迭代器{typedef ForwardIteratorTag CategoryIterator;typedef T ValueType;typedef Distance DifferenceType;typedef T* Pointer;typedef T& Reference;};template<class T, class Distance>struct BidirectionalIterator //雙向迭代器{typedef BidirectionalIteratorTag CategoryIterator;typedef T ValueType;typedef Distance DifferenceType;typedef T* Pointer;typedef T& Reference;};template<class T, class Distance>struct RandomAccessIterator //隨機迭代器{typedef RandomAccessIteratorTag CategoryIterator;typedef T ValueType;typedef Distance DifferenceType;typedef T* Pointer;typedef T& Reference;};
這5個迭代器中的繼承關係使雙向迭代器的對象可以傳給前向迭代器,隨機迭代器的對象可以傳給前向迭代器和雙向迭代器,它的原理就是“切片規則”。這就是爲什麼隨機迭代器可以隨機機訪問迭代器的內容。
三:迭代器萃取機STL中有那麼多的迭代器,它是通過什麼保證程序可以正確的調用合適的迭代器呢?
<span style="font-family:Microsoft YaHei;font-size:14px;">template<class Iterator>
struct IteratorTraits //Iterator類有內嵌類型
{
typedef typename Iterator::CategoryIterator IteratorCategory;
typedef typename Iterator::ValueType ValueType;
typedef typename Iterator::DifferenceType DifferenceType;
typedef typename Iterator::Pointer Pointer;
typedef typename Iterator::Reference Reference;
};</span><pre name="code" class="cpp"><span style="font-family:Microsoft YaHei;font-size:14px;">//偏特化原生的類型,爲了原生類型而存在的特化
template<class T>
struct IteratorTraits<T*>
{
typedef RandomAccessIteratorTag IteratorCategory;
typedef T ValueType;
typedef ptrdiff_t DifferenceType;
typedef T* Pointer;
typedef T& Reference;
};
//如上
template<class T>
struct IteratorTraits<const T*>
{
typedef RandomAccessIteratorTag IteratorCategory;
typedef T ValueType;
typedef ptrdiff_t DifferenceType;
typedef const T* Pointer;
typedef const T& Reference;
};
</span>
typename關鍵字告訴編譯器Iterator是一個類型,實例化出對象的時候在來編譯。這是class關鍵字不具有的功能。這裏有三種類型萃取機,他們分別代表三種不同的類型。
內置類型,原生指針類型,const原生指針類型。
<span style="font-family:Microsoft YaHei;font-size:14px;">template <class InputIterator>
inline typename IteratorTraits <InputIterator>::DifferenceType
Distance(InputIterator first, InputIterator last)
{
//通過傳入的InputIterator決定是哪個類型的IteratorCategory對象
return _Distance(first, last, IteratorTraits <InputIterator>::IteratorCategory());
}
//前向和雙向迭代器的Distance函數,都是通過遍歷兩個迭代器之間的元素實現的
template <class InputIterator>
inline typename IteratorTraits <InputIterator>::DifferenceType
_Distance(InputIterator first, InputIterator last, InputIteratorTag)
{
IteratorTraits<InputIterator >::DifferenceType n = 0;
while (first != last) {
++first; ++n;
}
return n;
}
//隨機迭代器的distance
template <class RandomAccessIterator>
inline typename IteratorTraits <RandomAccessIterator>::DifferenceType
_Distance(RandomAccessIterator first, RandomAccessIterator last,RandomAccessIteratorTag)
{
return last - first;
}</span>
<span style="font-family:Microsoft YaHei;">template <class InputIterator, class Distance>
inline void Advance(InputIterator & i, Distance n)
{
_Advance(i, n, IteratorTraits <InputIterator>::IteratorCategory());
}
template <class InputIterator, class Distance>
inline void _Advance(InputIterator & i, Distance n, InputIteratorTag)
{
while (n--) ++i;
}
template <class BidirectionalIterator, class Distance>
inline void _Advance(BidirectionalIterator & i, Distance n,
BidirectionalIteratorTag)
{
if (n >= 0)
while (n--) ++i;
else
while (n++) --i;
}
template <class RandomAccessIterator, class Distance>
inline void __Advance(RandomAccessIterator & i, Distance n,
RandomAccessIteratorTag)
{
i += n;
} </span>
advance的提取方法和distance相同。
所以我們可以看到,模板是爲STL而生的一點都不爲過。通過三個萃取機IteratorTraits可以將原生的內置類型和自定義的類型分開是萃取機的精華所在。