- 定义:
- 是类的对象,用来遍历容器的元素,可被视作指向容器元素的指针。
- 为获取不同容器元素提供了统一接口(Iterators are central to generic programming because they are an interface between containers and algorithms)
- 类型:
- container::iterator (read/write iterator)
- container::const_iterator (read-only iterator)
- 操作:(引自:http://www.fredosaurus.com/notes-cpp/stl-iterators/iter-ops.html)
- Assume C is a container class, containing elements of type T.
-
bool b; int i; T value; C::iterator it, it1, it2;
-
Result Operator Description Operators for most iterators. Vectors, lists, arrays, .... value =
*it;
Use dereference (*) op to get/set value. ++it;
Points to next element. Value after update. it++;
Points to next element. Value before update. it1 =
it2;
Assignment b =
it1 == it2;
Equality comparison. b =
it1 != it2;
Inequality. Additional operators for bidirectional iterators. Vectors, lists, arrays, ... --it;
Predecrement. it--;
Postdecrement. May be less efficient than predecrement. Additional operators for random-access iterators. Vectors and arrays, but not lists. it +=
i;
Increments it by i positions. it -=
i;
Decrements it by i positions. it1 =
it2 + i;
Increments it by i positions. it1 =
it2 - i;
Decrements it by i positions. value =
it[i];
Returns reference to ith element after it. b =
it1 < it2;
Comparison. b =
it1 <= it2;
Comparison. b =
it1 > it2;
Comparison. b =
it1 <= it2;
Comparison.