4 ms·
<algorithm> is so 90s. In C++14 you'll be able to do this: auto square_copy = [](auto v) { for (auto& e: v) { e *= e; } return v; }; Ano
by nly 13y ago
<algorithm> is so 90s. In C++14 you'll be able to do this:
auto square_copy = [](auto v) {
for (auto& e: v) { e *= e; }
return v;
};
Another option:
auto square = [](auto& v) { v *= v; };
auto map = [](auto range, auto fun) {
transform (begin(range), end(range), fun);
return range;
};
vector<int> vec = {1,2,3,4,5,6};
auto squared = map (vec, square);
Note that, in both of these cases, if you use std::move() to gift a container to these functions, they will not actually allocate any memory and will effectively operate in-place.
- stormbrew 13y agoAm I missing something? I don't see anything beyond C++11 in here.
- nly 13y agoParameters can't be auto in C++11. Polymorphic lambdas are a C++14 feature. Incidentally, are you Stormhawk on Freenodes #C++ ?
- stormbrew 13y agoAh right. I have to admit I was so astounded this was omitted in C++11 (with some really dumb rationale, iirc) that I keep forgetting it wasn't. And nope, that's not me. I go by stormbrew on freenode as well and haven't been in a C++ focused channel since the late 90s on efnet. :)
- nly 13y agoNot quite as astounding as omitting make_unique :P No matter, C++14 is going to be here very soon. Both of the open source compilers are almost there, and I've read the standard will be final once the Ts are crossed.