v is a vector of doubles {0.5, 0.5, 0.5}. What does std::accumulate(v.begin(), v.end(), 0) return?
0 + 0.5 = 0.5 is stored back into an int as 0, each time. Use 0.0.
STL Algorithms and Iterators · Exercise 1 of 4
Twenty multiple-choice questions on C++ STL algorithms and iterators: iterator categories, find and count, sort variants, binary search, remove-erase, numeric algorithms and ranges. Pick an option to check your answer instantly.
v is a vector of doubles {0.5, 0.5, 0.5}. What does std::accumulate(v.begin(), v.end(), 0) return?
0 + 0.5 = 0.5 is stored back into an int as 0, each time. Use 0.0.
Which task is std::nth_element best for?
Everything before the n-th position is ≤ it and everything after ≥ it, but not sorted.
Why is [](int a, int b) { return a <= b; } wrong as a std::sort comparator?
Comparators must behave like <.
What category are std::list iterators?
They support ++ and -- but not it + n; use std::next.
What does std::transform(a.begin(), a.end(), out, f) do?
The output may be the input itself for an in-place transformation.
In std::ranges::sort(v, {}, &Student::marks), what is &Student::marks?
{} selects the default comparator, std::ranges::less.
When are the elements of v | std::views::filter(f) | std::views::transform(g) computed?
Views do not own data; they compute on demand.
What is the key difference between std::accumulate and std::reduce?
With string concatenation or floating-point sums the results may differ.
To print every permutation of a string with std::next_permutation, what must you do first?
next_permutation returns false after the largest permutation (and resets to the smallest).
What is the complexity of std::distance(first, last)?
The algorithm adapts to the iterator category.
Timed online tests with instant scores and explanations.