ranges of ranges to vector of vectors
You can use ranges::to
to convert the range of ranges into a vector of vectors. For example:
#include <vector>
#include <iostream>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/all.hpp>
#include <range/v3/view/group_by.hpp>
#include <range/v3/view/transform.hpp>
int main() {
std::vector<int> rng {0,1,2,3,4,5,6,7,8,9};
auto groups = ranges::view::group_by(rng, [](int i, int j){
return j/3 == i/3;
});
auto vs = groups | ranges::to<std::vector<std::vector<int>>>;
// Display the result: [[0,1,2],[3,4,5],[6,7,8],[9]]
std::cout << ranges::view::transform(vs, ranges::view::all) << std::endl;
}
June 10, 2020: Previously, this answer recommended simply assigning from groups
to a vector<vector<int>>
variable because range-v3 used to support implicit conversions from views to containers. The implicit conversions were problematic, and so they were dropped. ranges::to
is now the idiomatic way to do this.
Assuming you are using Rangesv3, my reading of the docs gives me something like this:
auto groups = ranges::view::group_by(rng, bin_op)
| ranges::view::transform( ranges::to_vector )
| ranges::to_vector;
or maybe
auto groups = ranges::view::group_by(rng, bin_op)
| ranges::view::transform( [] (auto r) { return r | ranges::to_vector; } )
| ranges::to_vector;
(I recall that ranges::to_vector
could be used in a function-style way, but I could be wrong, or things could have changed. The first one assumes it can be; the second doesn't.)
What this does is it first transforms the lazy range of ranges into a lazy range of vectors.
It then transforms the lazy range of vectors into a vector of vectors.
This works better (inside-out) because the intermediate products are lazy "on the outside". There may be a way to do it from the outside-in, but a vector of lazy ranges has to actually exist in a way that a lazy range of vectors does not.