L6.Iterators
约 301 个字 18 行代码 3 张图片 预计阅读时间 1 分钟
Iterator Basics¶
Iterator knows where the next element is,even if the elements are stored haphazardly.
The STL iterators support the following operations:
- Copy constructible and assignable (
iter = another_iter
) - Retrieve current element (
*iter
) - Advance iterator (
++iter
) - Equality comparable (
iter != container.end()
)
The STL containers support the following operations:
begin()
: iterator to the first elementend()
: iterator to one past the last element- So deferencing or advancing the end iterator is undefined behavior
Example
Print all elements the following collections
Iterator Categories¶
All iterators must support:
- Copy construction and assignment
- Equality comparable ( ==
and !=
)
- Dereferencable (*iter
) - either readable or writable (not necessarily both)
- Incrementable (++iter
) - sign pass (previous iter invalidated after ++)
Supported Operations:
- Copy construct/assign (
=
) + destructible - Default constructible
- Equality comparable (
==
and!=
) - Dereferencable r-value (
auto e = *iter, ->
) - Dereferencable l-value (
*iter = e
) - Single-pass Incrementable (
++iter
) - Multi-pass Incrementable
- Decrementable (
--iter
) - Random access (
+,-,+=,-=,offset[]
) - Inequality comparable (
<,>,<=,>=
)
Erase and Iterator Invalidation¶
Example : remove first element that begin with 'A'
C++ | |
---|---|
How about remove all elements that begin with 'A'?
C++ | |
---|---|
Iterator Invalidation¶
Vector¶
Documentation tells you which operations invalidate which iterators.
So that's why we say "std::list
is stable"
Adding,removing and moving the elements within the list or across serval lists does not invalidate the iterators or references.An iterator is invalidated only when the corresponding element is deleted.
Return Value¶
Iterator following the last removed element(返回值是更新后的容器的被删除元素的后一个元素的迭代器).
- If pos refers to the last element, then the
end()
iterator is returned. - If
last == end()
prior to removal, then the updatedend()
iterator is returned. - If
[first, last)
is an empty range, then last is returned.
So after knowing the return value of erase()
, we may find that the code above is wrong.
Here we just provide a simple solution to remove all elements that begin with 'A'.
C++ | |
---|---|
If we erased something,then we don't increment.Otherwise,we increment.