4 ms·
I'm not really sure why references are being used here, if you're just making a copy straight-away. vector<int> square(vector<int> vcopy) { for
by jeorgun 13y ago
I'm not really sure why references are being used here, if you're just making a copy straight-away.
vector<int> square(vector<int> vcopy)
{
for (int &i : vcopy)
i *= i;
return vcopy;
}
is shorter, (IMO) much more readable, and on my tests, measurably faster than the reference version. If you really want the std::accumulate version,
vector<int> square(vector<int> vcopy)
{
accumulate(begin(vcopy), end(vcopy), begin(vcopy),
[](int i) { return i * i; });
return vcopy;
}
works just as well.
Still, I guess it's beside the point.
- humanrebar 13y agoI like your version better, but I suspect it would be significantly slower for many compilers that you are not testing.
- Dobiasd 13y agoAwesome! It is more readable and makes it faster. Thank you very much. I changed my article accordingly.