迭代器是对指针进行进一步抽象的产物。
迭代器是遍历所有容器(序列)/流的统一界面,是标准库泛形算法的基础。
迭代器根据其能力分为五种:
category | properties | valid expressions | |||
---|---|---|---|---|---|
all categories | copy-constructible, copy-assignable and destructible | X b(a); |
|||
Can be incremented | ++a |
||||
Random Access | Bidirectional | Forward | Input | Supports equality/inequality comparisons | a == b |
Can be dereferenced as an rvalue | *a a->m |
||||
Output | Can be dereferenced as an lvalue (only for mutable iterator types) |
*a = t *a++ = t |
|||
default-constructible | X a; X() |
||||
Multi-pass: neither dereferencing nor incrementing affects dereferenceability Multi-pass是指,迭代器不管进行多少次自增及解引用,都不会使得其指过的对象无法访问。 |
{ b=a; *a++; *b; } |
||||
Can be decremented | --a a-- *a-- |
||||
Supports arithmetic operators + and - | a + n n + a a - n a - b |
||||
Supports inequality comparisons (<, >, <= and >=) between iterators | a < b a > b a <= b a >= b |
||||
Supports compound assignment operations += and -= | a += n a -= n |
||||
Supports offset dereference operator ([]) | a[n] |
时间: 2024-10-12 16:06:30