Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

(C++) std::ptr_fun

 

An adapter to be able to use the algorithm for_each on a function of T stored in a container as T, instead of using loops. Prefer algorithm calls over hand-written loops [0] [1] .

 

Example

 

 

#include <algorithm>

#include <cctype>

#include <string>

 

//From http://www.richelbilderbeek.nl/CppStrToUpper.htm

const std::string StrToUpper (std::string s)

{

std::transform(s.begin(), s.end(), s.begin(),std::ptr_fun(std::toupper));

return s;

}

 

 

 

 

 

 

References

[0]   Bjarne Stroustrup . The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4. Chapter 18.12.1: 'Prefer algorithms to loops.

[1]   Scott Meyers . Effective STL. ISBN:0-201-74962-9. Item 43: 'Prefer algorithm calls over hand-written loops'

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.