std::bind1st,std::bind2nd(3)
π NAME π
std::bind1st, std::bind2nd β bestie, this is the std::bind1st,std::bind2nd(3) page from the C++ Standard Libary (yes, that spelling is literally in the header, don't come for me π). Itβs the old-school pair for binding one argument to a binary function object.
β¨ Synopsis β¨
Okay so first thing: itβs defined in header <functional> π«Ά
Defined in header <functional>
template< class F, class T > (1) (deprecated in C++11)
std::binder1st<F> bind1st( const F& f, const T& x ); (removed in C++17)
template< class F, class T > (2) (deprecated in C++11)
std::binder2nd<F> bind2nd( const F& f, const T& x ); (removed in C++17)
So the vibe is: it binds a given argument x to a first or second parameter of the given binary function object f. That is, stores x within the resulting wrapper, which, if called, passes x as the first or the second parameter of f. Basically itβs holding your little argument for you and handing it over at the right spot. Periodt π
- Binds the first argument of
ftox. Effectively callsstd::binder1st<F>(f, typename F::first_argument_type(x)). - Binds the second argument of
ftox. Effectively callsstd::binder2nd<F>(f, typename F::second_argument_type(x)).
π Parameters π
fβ pointer to a function to bind an argument toxβ argument to bind tof
πΈ Return value πΈ
A function object wrapping f and x. Simple as that, icon β¨
π Exceptions π
May throw implementation-defined exceptions. No cap.
π¦ Example π¦
The original has a little // Run this code marker at the top, so Iβm keeping it β hereβs the whole thing, bestie:
// Run this code
#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;
}
β Output β
This is giving exactly:
0Β° = 0.000000 rad
30Β° = 0.523599 rad
45Β° = 0.785398 rad
60Β° = 1.047198 rad
90Β° = 1.570796 rad
180Β° = 3.141593 rad
π See also π
binder1st,binder2ndβ function object holding a binary function and one of its arguments (class template) (deprecated in C++11)(removed in C++17)bind_frontβ bind a variable number of arguments, in order, to a function object (function template) (C++20)bind_backβ bind a variable number of arguments, in order, to a function object (function template) (C++23)
source: http://cppreference.com Β· version/date: 2024.06.10 β mwah, bestie ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms