I do not understand the result of the following code (C++20 compiled with g++-10):
#include <iostream>
#include <vector>
#include <algorithm>
#include <ranges>
int main() {
using namespace std;
auto v = vector<int>{0,1,2,3,4,5,6,7};
auto r = v | views::filter([](int n) {return n % 3 == 0;});
ranges::generate(r,[]() { return 2; });
auto s = v | views::filter([](int n) {return n % 3 == 0;});
cout << "v: ";
for(auto x: v) cout << x << " ";
cout << "\n";
cout << "r: ";
for (auto x: r) cout << x << " " ;
cout << "\n";
cout << "s: ";
for (auto x: s) cout << x << " " ;
cout << "\n";
}
On my machine, the result is:
v: 2 1 2 2 4 5 2 7
r: 2
s:
I understand the first (v) and the third lines (s): in v, integers divisible by 3 are replaced by 2 (first line) and thus afterwards no integer is divisible by 3 anymore. Thus the third line is empty.
But why does the second line (r) display a unique value 2 and is different from the third line ???
If I replace the 2 by 3 in the ranges::generate then the result is normal:
v: 3 1 2 3 4 5 3 7
r: 3 3 3
s: 3 3 3
Maybe the reason is trivial but I did not find any reason in the documentation...