STL是标准C++的组成,不熟悉STL不叫懂C++,我比较赞成这个说法的。
容器和迭代器是STL中的两个重要的概念。
这里摘述D.S.Malik的《数据结构——C++版》中的一句话:
每一个容器(序列或者关联)都包括一个typedef iterator。
这句话阐述了容器与迭代器之间的一个十分重要的关系,即:
容器中是有绑定迭代器的,或者说,容器内置了迭代器(实际用的是typedef)。
看如下代码:
#include <iostream> #include <list> #include <deque> #include <vector> using namespace std; int main(){ list<int> l; list<int>::iterator ite1; vector<int> v; vector<int>::iterator ite2; deque<int> d; deque<int>::iterator ite3; return 0; }
可以看到,list,vector和deque都内置了iterator,我们使用的时候直接按模式使用就可以了。
有点要注意,有很多容器是没有内置的,比如queue,这和容器的特点有关,queue根本就不提供遍历的特点,所以不提共itarator是可以理解的。