How to use bind function with placeholders in C++

4 Answers

0 votes
#include <iostream> 
#include <functional> 

using namespace std; 
  
void f(int a, int b) { 
    cout << a << " " << b << endl; 
} 
  
int main() 
{ 
    using namespace std::placeholders;
    
    auto fn =  bind(f, _1, 33); 
  
    fn(100); 

    return 0; 
} 



/*
run:

100 33

*/

 



answered Feb 6, 2020 by avibootz
0 votes
#include <iostream> 
#include <functional> 

using namespace std; 
  
void f(int a, int b) { 
    cout << a << " " << b << endl; 
} 
  
int main() 
{ 
    using namespace std::placeholders;
    
    auto fn =  bind(f, 99, _1); 
  
    fn(20); 

    return 0; 
} 



/*
run:

99 20

*/

 



answered Feb 6, 2020 by avibootz
0 votes
#include <iostream> 
#include <functional> 

using namespace std; 
  
void f(int a, int b) { 
    cout << a << " " << b << endl; 
} 
  
int main() 
{ 
    using namespace std::placeholders;
    
    auto fn =  bind(f, _1, _2); 
  
    fn(16, 98); 

    return 0; 
} 



/*
run:

16 98

*/

 



answered Feb 6, 2020 by avibootz
0 votes
#include <iostream> 
#include <functional> 

using namespace std; 
  
void f(int a, int b) { 
    cout << a << " " << b << endl; 
} 
  
int main() 
{ 
    using namespace std::placeholders;
    
    auto fn =  bind(f, _2, _1); 
  
    fn(16, 98); 

    return 0; 
} 



/*
run:

98 16

*/

 



answered Feb 6, 2020 by avibootz
...