-
- Downloads
Fix intro_algorithms notebook
%% Cell type:markdown id: tags: | ||
# Operations on sequences with STL algorithms | ||
The following example demonstrates how to create and manipulate sequences using the algorithms in the header "algorithm". Try to predict the output when you execute the code in each of the cells below. Then execute the cell (Ctrl+Enter) and examine output, try to understand what the different algorithms do. | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
using namespace std; | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
vector v{1, 2, 3, 4, 5, 6, 7, 8, 9}, w{4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; | ||
vector<int> x, y, z, m; | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
if (is_sorted(begin(v), end(v))) | ||
cout << "The sequence is sorted in increasing order.\n"; | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
reverse(v.begin(), v.end()); | ||
v | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
v | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
rotate(v.begin(), v.begin()+3, v.end()); | ||
v | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
sort(begin(v), end(v)); | ||
v | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
merge(v.begin(), v.end(), w.begin(), w.end(), back_inserter(m)); | ||
m | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
set_union(begin(v), end(v), begin(w), end(w), back_inserter(x)); | ||
x | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
set_intersection(w.begin(), w.end(), v.begin(), v.end(), back_inserter(y)); | ||
y | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
set_symmetric_difference(v.begin(), v.end(), w.begin(), w.end(), back_inserter(z)); | ||
z | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
if (is_permutation(z.begin(), z.end(), v.begin(), v.end())) | ||
cout << "z is a permutation of v\n"; | ||
else | ||
cout << "z is not a permutation of v\n"; | ||
``` | ||
%% Cell type:markdown id: tags: | ||
## Part 2: | ||
Many algorithms accept one or more "callable" objects as arguments. This could be a simple function as shown here ... | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
void print_it(int x) { std::cout << x << "\n"; } | ||
for_each(begin(v), end(v), print_it); | ||
``` | ||
%% Cell type:markdown id: tags: | ||
The callable object could also be a class with an overloaded `operator()` ... | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
struct show_squared { | ||
void operator()(int x) const noexcept { std::cout << x * x << "\n"; } | ||
}; | ||
for_each(begin(v), end(v), show_squared{}); | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
for_each(begin(v), end(v), show_squared{}); | ||
``` | ||
%% Cell type:markdown id: tags: | ||
Alternatively, one could use a lambda function ... | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
for_each(begin(v), end(v), [](int x){ std::cout << x * x << "\n"; }); | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
y.clear(); | ||
copy(x.begin(), x.end(), back_inserter(y)); | ||
y | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
z.clear(); | ||
copy_if(x.begin(), x.end(), back_inserter(z), [](auto i){ return (i % 7) < 3; }); | ||
z | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
z.clear(); | ||
set_symmetric_difference(begin(v), end(v), begin(w), end(w), back_inserter(z)); | ||
z | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
auto doubledigitsstart = find_if(begin(z), end(z), [](auto i){ return i > 9 or i < -9; }); | ||
vector<int> filtered; | ||
copy(doubledigitsstart, end(z), back_inserter(filtered)); | ||
filtered | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
x = v; | ||
reverse(x.begin(), x.end()); | ||
y.clear(); | ||
transform(begin(x), end(x), begin(v), back_inserter(y), [](auto i, auto j){ return i+j; }); | ||
y | ||
``` | ||
%% Cell type:code id: tags: | ||
``` C++ | ||
``` c++ | ||
``` | ||
... | ... |
Please register or sign in to comment