Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) StdFunctionExample1

 

C++11STLQt CreatorLubuntu

 

std::function example 1: global functions is a std::function example.

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

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
}

 

 

 

 

 

./CppStdFunctionExample1/main.cpp

 

#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...

*/

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml