I have a vector and I would like to multiply each of its elements with a scalar value. I wrote the following code for this
int func(const int x)
{
return 10 * x; // say the scalar value is 10
}
void foo()
{
// .. compute vector vert
std::transform(vert.begin(), vert.end(), vert.begin(), ::func);
}
The code does the job, but I was wondering if there is a more concise implementation possible for this, one that does not require me to define the func function seperately
I still have yet to see a case where transform()
actually looks like a good answer... I would strongly suggest:
for (int& x : vert) {
x *= 10; // or x = func(x)
}
It's clearer and more concise.
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.