Namespaces
Variants
Views
Actions

Talk:cpp/algorithm/transform

From cppreference.com

Is the <functional> header really needed in the Example? — Preceding unsigned comment added by 95.49.59.138 (talkcontribs)

no, it's a leftover from when the example used std::ptr_fun. --Cubbi (talk) 11:46, 23 January 2016 (PST)

Excuse me, but what was wrong with my edit and why did it have to be "fix"ed like this: http://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/transform&curid=789&diff=83193&oldid=83192 ? AFAIK my edit was correct… I resolved the ambiguity by casting toupper to the right pointer to function type. user:‎T. Canens? — Preceding unsigned comment added by 95.49.59.138 (talkcontribs)

Anything passed to the <cctype> functions should be first be converted to unsigned char. Otherwise, if char is signed and the value is negative, you get undefined behavior. It's not strictly necessary here, but I'd rather not show questionable practices. T. Canens (talk) 13:44, 23 January 2016 (PST)

[edit] Example conflicts with note

In the "Notes" sections it says that "to apply a function that modifies the elements of a sequence" we should prefer `std::for_each`. However, the example below does just that with

   std::transform(s.begin(), s.end(), s.begin(), ...)

Is this use good practice? If so, should we clarify why?

Mleoni (talk) 06:18, 20 April 2022 (PDT)

The function itself isn't modifying the element that's passed to it (which would require taking a reference parameter), which is what I believe the note refers to. --Ybab321 (talk) 06:46, 20 April 2022 (PDT)