Let's say that given a range like this
std::vector<int> v{1, 4, 7, 2};
I want to generate another range where all even numbers are repeated a number of times equal to their value, whereas all odd numbers are left unchanged.
A possible solution is the following:
#include <iostream>
#include <range/v3/view/join.hpp>
#include <range/v3/view/transform.hpp>
#include <range/v3/view/repeat_n.hpp>
#include <vector>
using namespace ranges::views;
auto f = [](auto x){
return (x % 2) ? repeat_n(x,1) : repeat_n(x,x);
};
int main() {
std::vector<int> v{1, 4, 7, 2};
auto w = v | transform(f) | join;
for (auto i : w)
std::cout << i << std::endl;
}
However, using repeat_n(x,1)
just in order to wrap x
in a singleton range looks a bit clunky to me. Is there some ad-hoc function in Range-v3 to do this?