Skip to content

Commit 61d9ae8

Browse files
Extend STL examples on the slides
1 parent ec91536 commit 61d9ae8

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

talk/morelanguage/stl.tex

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,42 @@
260260
\begin{frame}[fragile]
261261
\frametitlecpp[14]{More examples}
262262
\begin{cppcode}
263-
// Finds the first element in a list between 1 and 10.
264263
std::list<int> l = ...;
265-
...
264+
265+
// Finds the first element in a list between 1 and 10.
266266
const auto it =
267267
std::find_if(l.begin(), l.end(),
268268
[](int i) { return i >= 1 && i <= 10; });
269+
if (it != l.end())
270+
int element = *it;
269271

270272
// Computes sin(x)/(x + DBL_MIN) for elements of a range.
271-
std::transform(first, last, first,
273+
std::vector<double> r(l.size());
274+
std::transform(l.begin(), l.end(), r.begin(),
272275
[](auto x) { return sin(x)/(x + DBL_MIN); });
276+
277+
// reduce/fold (using addition)
278+
const auto sum = std::reduce(v.begin(), v.end());
279+
\end{cppcode}
280+
\end{frame}
281+
282+
\begin{frame}[fragile]
283+
\frametitlecpp[14]{More examples}
284+
\begin{cppcode}
285+
std::vector<int> v = ...;
286+
287+
// remove duplicates
288+
std::sort(v.begin(), v.end());
289+
auto newEndIt = std::unique(v.begin(), v.end());
290+
v.erase(newEndIt, v.end());
291+
292+
// remove by predicate
293+
auto p = [](int i) { return i > 42; };
294+
auto newEndIt = std::remove_if(v.begin(), v.end(), p);
295+
v.erase(newEndIt, v.end());
296+
297+
// remove by predicate (C++20)
298+
std::erase_if(v, p);
273299
\end{cppcode}
274300
\end{frame}
275301

0 commit comments

Comments
 (0)