C++: Add section on iterators
This commit is contained in:
parent
594eeadc16
commit
f2167f7ee2
1 changed files with 21 additions and 1 deletions
|
@ -87,7 +87,7 @@
|
||||||
<section id="sect-Defensive_Coding-CXX-Std-Subscript">
|
<section id="sect-Defensive_Coding-CXX-Std-Subscript">
|
||||||
<title>Containers and <literal>operator[]</literal></title>
|
<title>Containers and <literal>operator[]</literal></title>
|
||||||
<para>
|
<para>
|
||||||
Many containers similar to <literal>std::vector</literal>
|
Many sequence containers similar to <literal>std::vector</literal>
|
||||||
provide both <literal>operator[](size_type)</literal> and a
|
provide both <literal>operator[](size_type)</literal> and a
|
||||||
member function <literal>at(size_type)</literal>. This applies
|
member function <literal>at(size_type)</literal>. This applies
|
||||||
to <literal>std::vector</literal> itself,
|
to <literal>std::vector</literal> itself,
|
||||||
|
@ -105,5 +105,25 @@
|
||||||
slightly more verbose.
|
slightly more verbose.
|
||||||
</para>
|
</para>
|
||||||
</section>
|
</section>
|
||||||
|
<section id="sect-Defensive_Coding-CXX-Std-Iterators">
|
||||||
|
<title>Iterators</title>
|
||||||
|
<para>
|
||||||
|
Iterators do not perform any bounds checking. Therefore, all
|
||||||
|
functions that work on iterators should accept them in pairs,
|
||||||
|
denoting a range, and make sure that iterators are not moved
|
||||||
|
outside that range. For forward iterators and bidirectional
|
||||||
|
iterators, you need to check for equality before moving the
|
||||||
|
first or last iterator in the range. For random-access
|
||||||
|
iterators, you need to compute the difference before adding or
|
||||||
|
subtracting an offset. It is not possible to perform the
|
||||||
|
operation and check for an invalid operator afterwards.
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
|
Output iterators cannot be compared for equality. Therefore, it
|
||||||
|
is impossible to write code that detects that it has been
|
||||||
|
supplied an output area that is too small, and their use should
|
||||||
|
be avoided.
|
||||||
|
</para>
|
||||||
|
</section>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue