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* )
헤더 파일에 정의됨 <functional>
template < class F, class T >
std:: binder1st < F > bind1st ( const F & f, const T & x ) ;
(1) (C++11에서 사용 중단됨)
(C++17에서 제거됨)
template < class F, class T >
std:: binder2nd < F > bind2nd ( const F & f, const T & x ) ;
(2) (C++11에서 사용 중단됨)
(C++17에서 제거됨)

주어진 인수 x 를 주어진 이항 함수 객체 f 의 첫 번째 또는 두 번째 매개변수에 바인딩합니다. 즉, x 를 결과 래퍼 내에 저장하며, 이 래퍼가 호출될 경우 x f 의 첫 번째 또는 두 번째 매개변수로 전달합니다.

1) f 의 첫 번째 인수를 x 에 바인딩합니다. 효과적으로 std:: binder1st < F > ( f, typename F :: first_argument_type ( x ) ) 를 호출합니다.
2) f 의 두 번째 인수를 x 에 바인딩합니다. 효과적으로 std:: binder2nd < F > ( f, typename F :: second_argument_type ( x ) ) 를 호출합니다.

목차

매개변수

f - 인수에 바인딩할 함수에 대한 포인터
x - f 에 바인딩할 인수

반환값

f x 를 감싸는 함수 객체.

예외

구현 정의 예외를 던질 수 있습니다.

예제

#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); // C++20부터는 std::numbers::pi 사용
    std::transform(a.begin(), a.end(), r.begin(),
        std::bind1st(std::multiplies<double>(), pi / 180.0));
//  동등한 람다식: [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;
}

출력:

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

참고 항목

(C++11에서 사용 중단됨) (C++17에서 제거됨)
이항 함수와 그 인수 중 하나를 보유하는 함수 객체
(클래스 템플릿)
(C++20) (C++23)
가변 개수의 인수를 함수 객체에 순서대로 바인딩
(함수 템플릿)