module 4 Flashcards

(21 cards)

1
Q

Given int a[6] {1, 2, 3, 4, 5, 6}; use copy() to output each element of the array without using begin() and end()

A
copy(a, a + 6, ostream_iterator<int>(cout, " "));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Given int a[6] {1, 2, 3, 4, 5, 6}; use copy() to output each element of the array using begin() and end()

A
copy(begin(a), end(a), ostream_iterator<int>(cout, " "));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Non-mutating algorithms defined in <algorithm>

A
  • for_each
  • find
  • find_if
  • find_first_of
  • adjacent_find
  • count
  • count_if
  • mismatch
  • equal
  • search
  • find_end
  • search_n
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Mutating Algorithms defined in <algorithm>

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Sorting Algorithms defined in <algorithm>

A
  • sort
  • stable_sort
  • partial_sort
  • partial_sort_copy
  • nth_element
  • merge
  • inplace_merge
  • partition
  • stable_partition
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

set operations defined in <algorithm>

A
  • includes
  • set_union
  • set_intersection
  • set_difference
  • set_symmetric_difference
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Heap (priority queue) operations defined in <algorithm>

A
  • push_heap
  • pop_heap
  • make_heap
  • sort_heap
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Searching Algorithms defined in <algorithm>

A
  • binary_search
  • lower_bound
  • upper_bound
  • equal_range
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Min and Max algorithms defined in <algorithm>

A
  • min
  • max
  • min_element
  • max_element
  • lexicographical_compare
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Permutation algorithms defined in <algorithm>

A
  • next_permutation
  • prev_permutation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write a function called selection_sort() that takes two iterator pointer parameters, one to the beginning and one to the end.

#include <algorithm>

A
#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));
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Numeric Algorithms defined in <numeric>

A
  • accumulate(beg, end, init)
  • accumulate(beg, end, init, bin_function())
  • inner_product
  • partial_sum
  • adjacent_difference
  • iota
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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()

A
#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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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
}
A
template<class T>
struct Plus {
    T operator()(const T& m, const T& n) {
        return m+n;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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};
A
copy(begin(a), end(a), ostream_iterator(cout, " ")); 
// output -> 1 2 3 4 5 0 0 
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Copy the numbers from the following string into a vector without using a loop;

string s = "12 13 14 15 16 17";
A
    istringstream is(s);
    istream_iterator<int> b(is);
    istream_iterator<int> e;
    vector<int> a(b, e);
17
Q

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)>>);
}
A
    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;});
18
Q

Write a function, function<int(int)> addx(int) that returns a lambda function. Make the lambda function capture the addx() functions parameter.

A
function<int(int)> addx(int x) {
    return [x](int y)->int {return x+y;};
}
19
Q

Write a recursive lambda that executes the fibonacci sequence

A
function<int(int)> fib =
        [&fib](int n) {return n < 2 ? n : fib(n-1) + fib(n-2);};
20
Q

Lambda Capture Summary

A
  • [] - 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
21
Q

Write a reverse iterator to print the following vector backwards:

vector<int> lyst = {1, 2, 3, 4, 5};
A
auto p = lyst.rbegin();
while (p != lyst.rend()) {
      cout << *p << endl;
      \++p;
}