Skip to content

Latest commit

 

History

History
20 lines (15 loc) · 795 Bytes

File metadata and controls

20 lines (15 loc) · 795 Bytes

3.1 迭代器设计思维——STL 关键所在

STL 的中心思想在于:将容器和算法分开,彼此独立设计,最后再把他们胶合在一起。

以下是容器、算法、迭代器的合作展示。以算法 find() 为例,它接受两个迭代器和一个搜寻目标:

// 摘自 SGI<stl_algo.h>
template <class InputIterator, class T>
InputIterator find(InputIterator first, 
                   InputIterator last, 
                   const T& value) {
    while (first!= last && *first!= value) ++first;
    return first;
}

只要给予不同的迭代器,find() 便能够对不同的容器进行查找操作。

迭代器似乎依附在容器之下,是吗?有没有独立而泛用的迭代器?我们又该如何自行设计特殊的迭代器?