I think it's agreeable that it really doesn't matter, and that this is all largely semantics. It all decays down to the same exact thing, considering transform is defined as:
1 2
|
for(; _First != _Last; ++_First, ++_Dest)
*_Dest = _Func(*_First);
|
so this
1 2
|
auto func = [ab, rot_ab](char)->char { /*....*/ };
std::transform(beg, end, dest, func);
|
is the same thing as this
|
std::transform(beg, end, dest, func, [ab, rot_ab](char)->char{ /*...*/ });
|
which is the same thing as this
1 2 3
|
auto func = [ab, rot_ab](char)->char{ /*...*/ };
for(; beg != end; ++beg, ++dest)
*dest = function(*beg);
|
I don't think there's any notable performance hit by doing it one specific way over the next (although i am unaware of the memory requirements it takes to store a function, or the time it takes to allocate space for it, or if that's even necessary when creating pointers to functions.) And I do believe transform() was included as an algorithm to simplify the commonly used method of applying a function to a container, much like ranged based for, as cire mentioned.
Catfish3's preference is equally as readable as hamsterman's in my opinion. It's not like the lambda spans more then a few lines.
Semantics will be the death of us.
EDIT:
Now that i'm into it, i prefer just dealing with ASCII:
1 2 3 4 5 6 7 8 9 10 11 12
|
template<class InputIter, class OutputIter>
OutputIter CaesarCipher(InputIter beg, InputIter end, OutputIter dest, int shift)
{
return std::transform(beg, end, dest, [&shift](char &c)->char {
if(islower(c))
return (((c-97) + shift) % 26) + 97;
else if(isupper(c))
return (((c-65) + shift) % 26) + 65;
else
return c; //isn't a letter
});
}
|
That seems to use the least amount of operations.
And I suppose I like putting the lambda in the call to transform. It flaunts C++11, which is newer and therefore more badass. :D