#include #include #include #include #include int main() { // Binder mit Lambda-Ausdruck auto bruch = [](int x, int y) { return double(x)/y; }; auto reziprok = std::bind(bruch, std::placeholders::_2, std::placeholders::_1); // Umordnen von Parametern auto stammbruch = std::bind(bruch, 1, std::placeholders::_1); // Currying std::cout << bruch(3, 4) << " = 1/" << reziprok(3, 4) << '\n'; std::cout << "1/3 = " << stammbruch(3) << '\n'; std::cout << "1/3 = " << stammbruch(3, 5, 7, 9) << '\n'; // erlaubt, aber irreführend! /// @see http://stackoverflow.com/questions/13251976/why-do-objects-returned-from-bind-ignore-extra-arguments /// @see http://stackoverflow.com/questions/7394367/is-using-boostbind-to-pass-more-arguments-than-expected-safe // Binder mit Methodenzeigern auto add_space = std::bind(&std::string::push_back, std::placeholders::_1, ' '); std::vector morse = { ".-", "-...", "-.-.", "-..", "." }; std::for_each(begin(morse), end(morse), add_space); // for (auto& s : morse) add_space(s); for (auto s : morse) std::cout << s; std::cout << '\n'; }