std::function example 1: global functions is a std::function example.
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
STL: GNU ISO C++ Library, version
4.9.2
Qt project file: ./CppStdFunctionExample1/CppStdFunctionExample1.pro
TEMPLATE = app CONFIG += console CONFIG -= app_bundle CONFIG -= qt SOURCES += main.cpp # # # Type of compile # # CONFIG(debug, debug|release) { message(Debug mode) } CONFIG(release, debug|release) { message(Release mode) #Remove all asserts and TRACE DEFINES += NDEBUG NTRACE_BILDERBIKKEL } # # # Platform specific # # # # # Compiler flags # # QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra unix { QMAKE_CXXFLAGS += -Werror } # # # Boost # # unix { message(Unix: Boost already in include path) } win32 { message(Windows: add Boost to include path) INCLUDEPATH += \ E:/Projects/Libraries/boost_1_54_0 }
#include <algorithm> #include <functional> #include <iostream> #include <vector> int f1(const int i) { return i + 1; } int f2(const int i) { return i + 2; } int f3(const int i) { return i + 3; } int main() { std::vector<std::function<int(const int)> > v; v.push_back(f1); v.push_back(f2); v.push_back(f3); std::random_shuffle(v.begin(),v.end()); int x = 0; for(const std::function<int(const int)>& f: v) { x = f(x); std::cout << x << '\n'; } } /* Possible screen output: 1 4 6 Press <RETURN> to close this window... */

