(C++) std::mem\fun\ref
January 11, 2018 · View on GitHub
(C++)
std::mem_fun_ref
std::mem_fun_ref is an adapter to be able to use algorithms on a member function of T stored in a container as T (compare std::mem_fun, to use algorithms on a member function of T stored in a container as T* ).
#include <algorithm> #include <iostream> #include <vector> struct MyClass { void SayHello() const { std::cout << "Hello" << std::endl; } };
Screen output:
Hello Hello Hello
Full example
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 12.10 (quantal)
Qt Creator 2.5.2
- G++ 4.7.2
Libraries used:
STL: GNU ISO C++ Library, version
4.7.2
Qt project file: CppMem_fun_ref.pro
TEMPLATE = app CONFIG += console CONFIG -= qt SOURCES += main.cpp QMAKE_CXXFLAGS += -Wall -Wextra -Weffc++ -Werror
main.cpp
#include <algorithm> #include <iostream> #include <vector> struct MyClass { void SayHello() const { std::cout << "Hello" << std::endl; } }; int main() { std::vector<MyClass> v(3); std::for_each(v.begin(), v.end(), std::mem_fun_ref(&MyClass::SayHello)); }