module 4 Flashcards
(21 cards)
Given int a[6] {1, 2, 3, 4, 5, 6};
use copy()
to output each element of the array without using begin()
and end()
copy(a, a + 6, ostream_iterator<int>(cout, " "));
Given int a[6] {1, 2, 3, 4, 5, 6};
use copy()
to output each element of the array using begin()
and end()
copy(begin(a), end(a), ostream_iterator<int>(cout, " "));
Non-mutating algorithms defined in <algorithm>
- for_each
- find
- find_if
- find_first_of
- adjacent_find
- count
- count_if
- mismatch
- equal
- search
- find_end
- search_n
Mutating Algorithms defined in <algorithm>
- transform
- copy
- copy_if
- copy_backward
- swap
- iter_swap
- swap_ranges
- replace
- replace_if
- replace_copy
- replace_copy_if
- fill
- fill_n
- generate
- generate_n
- remove
- remove_if
- remove_copy
- remove_copy_if
- unique
- reverse
- reverese_copy
- rotate
- rotate_copy
- random_shuffle
Sorting Algorithms defined in <algorithm>
- sort
- stable_sort
- partial_sort
- partial_sort_copy
- nth_element
- merge
- inplace_merge
- partition
- stable_partition
set operations defined in <algorithm>
- includes
- set_union
- set_intersection
- set_difference
- set_symmetric_difference
Heap (priority queue) operations defined in <algorithm>
- push_heap
- pop_heap
- make_heap
- sort_heap
Searching Algorithms defined in <algorithm>
- binary_search
- lower_bound
- upper_bound
- equal_range
Min and Max algorithms defined in <algorithm>
- min
- max
- min_element
- max_element
- lexicographical_compare
Permutation algorithms defined in <algorithm>
- next_permutation
- prev_permutation
Write a function called selection_sort()
that takes two iterator pointer parameters, one to the beginning and one to the end.
#include <algorithm>
#include <algorithm> using namespace std; template<typename Iter> void selection_sort(Iter begin, Iter end) { for (; begin != end; ++begin) { iter_swap(begin, min_element(begin, end)); } }
Numeric Algorithms defined in <numeric>
- accumulate(beg, end, init)
- accumulate(beg, end, init, bin_function())
- inner_product
- partial_sum
- adjacent_difference
- iota
Write a predicate function that that takes an int
as a parameter and determines if a number in a vector<int>
is less than 19
, then use that function in a copy_if()
#include <algorithm> #include <vector> #include <iostream> using namespace std; int main() { vector<int> a = {3, 6, 9, 12, 18, 21, 24, 27, 30}; vector<int> b(a.size()); auto endb = copy_if(begin(a), end(a), begin(b), [](int x){return x < 19;}); }
predicate functions are functions that return a bool
Overload the function call operator()
in struct Plus
in order to execute the following code in main:
int main() { Plus<int> p; cout << p(2,3) << endl; // 5 Plus<string> p2; cout << p2("carrot","top") << endl; // carrottop }
template<class T> struct Plus { T operator()(const T& m, const T& n) { return m+n; } };
Output the contents of the following array a
with a space between each value without using a loop.
int a[7] {1, 2, 3, 4, 5};
copy(begin(a), end(a), ostream_iterator(cout, " ")); // output -> 1 2 3 4 5 0 0
Copy the numbers from the following string into a vector without using a loop;
string s = "12 13 14 15 16 17";
istringstream is(s); istream_iterator<int> b(is); istream_iterator<int> e; vector<int> a(b, e);
Write a vector of the type function<int(int,int)>
, three lamba functions to add to the vector, then run it against the following code:
template<class F> void apply(F f) { cout << f(2,3) << endl; } int main() { vector<function<int(int,int)>> funs; // write lambda functions here for_each(funs.begin(), funs.end(), &apply<function<int(int,int)>>); }
funs.push_back([](int x, int y){return x + y;}); funs.push_back([](int x, int y){return x * y;}); funs.push_back([](int a, int b){return a - b;});
Write a function, function<int(int)> addx(int)
that returns a lambda function. Make the lambda function capture the addx()
functions parameter.
function<int(int)> addx(int x) { return [x](int y)->int {return x+y;}; }
Write a recursive lambda that executes the fibonacci sequence
function<int(int)> fib = [&fib](int n) {return n < 2 ? n : fib(n-1) + fib(n-2);};
Lambda Capture Summary
-
[]
- the lambda cannot use any variables from the enclosing function -
[names]
- names = a comma-separated list of names local to the enclosing function, their rvalues are copied into the lambda by default -
[&]
- by reference capture list -
[=]
- by value capture list -
[&, names]
- a comma-separated list of names local to the enclosing function, they are passed by reference -
[=, names]
- a comma-separated list of names local to the enclosing function, they are passed by reference
Write a reverse iterator to print the following vector backwards:
vector<int> lyst = {1, 2, 3, 4, 5};
auto p = lyst.rbegin(); while (p != lyst.rend()) { cout << *p << endl; \++p; }