How is it possible to obtain the same results of
std::copy(std::begin(a), std::end(a), std::begin(b));
using range-v3, and possibly its nice syntax?
EDIT
The reason I was having troubles is the misunderstanding of how to correctly use ranges::copy
: the second argument must be an iterator , not a range object. My fault ;)
Nevertheless, I am still asking if some sort of syntactic sugar is available to perform a ranged assignment , like the following:
ranges::???(b) = a | op1 | op2 | ... ;
I have got two fixed size vectors (at runtime). I need to perform some complex transformation of the data in the first vector and store the results in the second vector. I need to keep the first vector and I do not want to create a new temporary vector.
using namespace std;
vector<double> a;
...
vector<double> b(a.size());
transform(begin(a), end(a), begin(b), complexFun);
auto transformation = a | ranges::view::transform(complexFun);
copy(begin(transformation), end(transformation), begin(b));
In this simple case, it is a bit unnecessary to do so. However, if more than one operation is involved, creating the range view then using std::copy
is particularly useful.
ranges::???(b) = a | ranges::view::transform(complexFun);
What I am expecting is that this feature already exists, and I am not able to find it.
How about:
ranges::transform(a, b.begin(), complexfun);
?
EDIT: ... or maybe
ranges::copy( a | ranges::views::transform(complexFun), begin(b) );
?
There are a couple of good ways to do this. First, if you don't yet have the destination vector
and you want to create it:
auto b = a | ranges::view::transform(complexFun) | ranges::to_vector;
Second, if you already have a destination vector
whose capacity you want to reuse:
b.clear(); // Assuming b already contains junk
b |= ranges::action::push_back(a | ranges::view::transform(complexFun));
In both cases, range-v3 is smart enough to reserve capacity in the destination vector for ranges::size(a | ranges::view::transform(complexFun))
elements to avoid copies due to reallocation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.