标准库定义了三种顺序容器类型:vector、list 和 deque
顺序容器适配器包括 stack、queue 和 priority_queue 类型
使用迭代器时,不要求容器类型相同。容器内的元素类型也可以不相同,只要它们相互兼容,能够将要复制的元素转换为所构建的新容器的元素类型,即可实现复制。
容器元素类型必须满足以下两个约束:
• 元素类型必须支持赋值运算。
• 元素类型的对象必须可以复制。
IO 库类型不支持复制或赋值。因此,不能创建存放 IO 类型对象的容器。
iter->mem (*iter).mem
// check that there are elements before dereferencing an iterator
// or calling front or back
if (!ilist.empty()) {
// val and val2 refer to the same element
list<int>::reference val = *ilist.begin();
list<int>::reference val2 = ilist.front();
// last and last2 refer to the same element
list<int>::reference last = *--ilist.end();
list<int>::reference last2 = ilist.back(); }
通常来说,除非找到选择使用其他容器的更好理由,否则 vector 容器都是最佳选择
string 类型提供容器操作意味着可将操纵 vector 对象的程序改写为操纵 string 对象。
string s("Hiya!");
string::iterator iter = s.begin();
while (iter != s.end())
cout << *iter++ << endl; // postfix increment: print old value