Embercode
✕
The STLQuick quiz

Pipelines

6 questions. Covers Streams and ranges.

Questions 1 to 5 of 6

  1. 1. What does this print? ``cpp std::vector<int> v = {1, 2, 3, 4, 5, 6}; auto r = v | std::views::filter([](int x) { return x % 3 == 0; }) | std::views::transform([](int x) { return x * 10; }); std::cout << std::accumulate(r.begin(), r.end(), 0) << ' ' << std::ranges::distance(r); ``
  2. 2. What does this print? ``cpp std::vector<double> v = {2.5, 2.5, 2.5}; int total = std::accumulate(v.begin(), v.end(), 0); std::cout << total; ``
  3. 3. What does this print? ``cpp std::vector<std::string> w = {"tea", "kettle", "cup", "saucer"}; auto p = w | std::views::filter([](const std::string& s) { return s.size() >= 4; }) | std::views::transform([](const std::string& s) { return s.front(); }); for (char c : p) std::cout << c; std::cout << std::ranges::distance(p); ``
  4. 4. Put these steps in the order that a pipeline totals the even amounts in a vector.

    Use the arrows to put the steps in order.

    1. 1.Pipe the vector into a filter that keeps the even amounts
    2. 2.Pipe the kept amounts into a transform that doubles each one
    3. 3.Pass the view to std::accumulate to get the sum
    4. 4.Start with the vector of amounts
  5. 5. Why can std::ranges::sort not sort a filter view directly?

4 question(s) left on this page.