Namespaces
Variants

std:: bind1st, std:: bind2nd

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
bind1st bind2nd
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
Definiert im Header <functional>
template < class F, class T >
std:: binder1st < F > bind1st ( const F & f, const T & x ) ;
(1) (veraltet in C++11)
(entfernt in C++17)
template < class F, class T >
std:: binder2nd < F > bind2nd ( const F & f, const T & x ) ;
(2) (veraltet in C++11)
(entfernt in C++17)

Bindet ein gegebenes Argument x an einen ersten oder zweiten Parameter des gegebenen binären Funktionsobjekts f . Das heißt, speichert x innerhalb des resultierenden Wrappers, der bei Aufruf x als ersten oder zweiten Parameter an f übergibt.

1) Bindet das erste Argument von f an x . Ruft effektiv std:: binder1st < F > ( f, typename F :: first_argument_type ( x ) ) auf.
2) Bindet das zweite Argument von f an x . Ruft effektiv std:: binder2nd < F > ( f, typename F :: second_argument_type ( x ) ) auf.

Inhaltsverzeichnis

Parameter

f - Zeiger auf eine Funktion, an die ein Argument gebunden werden soll
x - Argument, das an f gebunden werden soll

Rückgabewert

Ein Funktionsobjekt, das f und x umschließt.

Ausnahmen

Kann implementierungsdefinierte Ausnahmen auslösen.

Beispiel

#include <algorithm>
#include <cmath>
#include <cstddef>
#include <functional>
#include <iomanip>
#include <iostream>
#include <vector>
int main()
{
    std::vector<double> a = {0, 30, 45, 60, 90, 180};
    std::vector<double> r(a.size());
    const double pi = std::acos(-1); // since C++20 use std::numbers::pi
    std::transform(a.begin(), a.end(), r.begin(),
        std::bind1st(std::multiplies<double>(), pi / 180.0));
//  an equivalent lambda is: [pi](double a) { return a * pi / 180.0; });
    for (std::size_t n = 0; n < a.size(); ++n)
        std::cout << std::setw(3) << a[n] << "° = " << std::fixed << r[n]
                  << " rad\n" << std::defaultfloat;
}

Ausgabe:

  0° = 0.000000 rad
 30° = 0.523599 rad
 45° = 0.785398 rad
 60° = 1.047198 rad
 90° = 1.570796 rad
180° = 3.141593 rad

Siehe auch

(deprecated in C++11) (removed in C++17)
Funktionsobjekt, das eine binäre Funktion und eines ihrer Argumente hält
(Klassen-Template)
(C++20) (C++23)
bindet eine variable Anzahl von Argumenten in Reihenfolge an ein Funktionsobjekt
(Funktions-Template)