Question 10
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
#include <iostream>
#include <ranges>
#include <vector>
int main() {
std::vector<int> v{1, 2, 3};
auto doubled = v | std::views::transform([](int x) { return x * 2; });
for (int& x : doubled) x = 0;
std::cout << v[0] << '\n';
return 0;
}
- A views cannot be stored in auto
- B transform_view produces new values (prvalues) computed on the fly, so int& cannot bind to them – there is nothing to write to Correct
- C v must be const
- D The lambda must return a reference
Correct Answer: Option B
To modify the vector, iterate it directly or use std::ranges::transform with v as the output.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!