Skip to content

Commit ad271eb

Browse files
Merge overleaf-2022-09-05-2021 into main
2 parents 974ad98 + cb4be6e commit ad271eb

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

chapters/02_introduction.tex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,20 @@ \section{A simpler mental model for iterators}
135135

136136
The less obvious can be the ranges returned by the algorithms. In some cases, the range is evident from the algorithm.
137137

138+
\begin{box-note}
139+
\footnotesize Example of \cpp{std::is_sorted_until} that returns an iterator to the first out-of-order element, which can also be thought as the end iterator for a maximal sorted sub-range.
140+
\tcblower
141+
\cppfile{code_examples/introduction/mental_sorted_code.h}
142+
\end{box-note}
138143

144+
The benefit of thinking about the returned value as the end iterator of a range is that it removes the potential for corner cases. For example, what if the algorithm doesn't find any element out of order? The returned value will be the end iterator of the source range, meaning that the range returned is simply the entire source range.
139145

140146
In some cases, a single returned iterator denotes multiple ranges.
141147

148+
\begin{box-note}
149+
\footnotesize Example of \cpp{std::lower_bound} that splits the range into two sub-ranges.
150+
\tcblower
151+
\cppfile{code_examples/introduction/mental_two_code.h}
152+
\end{box-note}
153+
142154
Even when the algorithms seem to return an iterator to a specific element, we can still think of the output as a range.

0 commit comments

Comments
 (0)