[STL][functional] std::bind, std::function
# std::bind, std::function
std::bind ν¨μ ν νλ¦Ώμ ν¨μλ ν¨μ κ°μ²΄λ₯Ό λ μμ μΈμ 리μ€νΈλ‘ λ°μΈλ©νμ¬ μλ‘μ΄ ν¨μλ ν¨μ κ°μ²΄λ₯Ό μμ±νλ€. μ΄λ 미리 μ§μ ν μΈμ κ°μ΄λ μΈμ μμΉμ κ°μ κ³ μ μν€λ λ° μ¬μ©λλ€. std::bindλ₯Ό μ¬μ©νλ©΄ μΈμλ₯Ό μΌλΆ κ³ μ νκ±°λ μμλ₯Ό λ³κ²½νμ¬ ν¨μ νΈμΆ μ μ μ°μ±μ λμΌ μ μλ€.
std::functionμ ν¨μ ν¬μΈν°μ λΉμ·ν κ°λ μΌλ‘, μ€ν κ°λ₯ν κ°μ²΄λ₯Ό λνλ΄λ ν¨μ κ°μ²΄(wrapper)μ΄λ€. λ€μν μ’ λ₯μ ν¨μλ ν¨μ κ°μ²΄λ₯Ό μ μ₯νκ³ νΈμΆν μ μμΌλ©°, νΉν λ°νμ μμ λ€λ₯Έ ν¨μλ ν¨μ κ°μ²΄λ₯Ό ν λΉνμ¬ μ¬μ©ν μ μλ€.
std::bindμ std::functionμ μλ‘ κΆν©μ΄ μ λ§λλ€. νΉν ν¨μν νλ‘κ·Έλλ°μμ κ°μ μ 보μΈλ€.
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
double divMe(double a, double b){
return double(a/b);
}
using namespace std::placeholders;
int main(){
std::cout << std::endl;
// invoking the function object directly
std::cout << "1/2.0= " << std::bind(divMe, 1, 2.0)() << std::endl;
// placeholders for both arguments
std::function<double(double, double)> myDivBindPlaceholder= std::bind(divMe, _1, _2);
std::cout << "1/2.0= " << myDivBindPlaceholder(1, 2.0) << std::endl;
// placeholders for both arguments, swap the arguments
std::function<double(double, double)> myDivBindPlaceholderSwap= std::bind(divMe, _2, _1);
std::cout << "1/2.0= " << myDivBindPlaceholderSwap(2.0, 1) << std::endl;
// placeholder for the first argument
std::function<double(double)> myDivBind1St= std::bind(divMe, _1, 2.0);
std::cout<< "1/2.0= " << myDivBind1St(1) << std::endl;
// placeholder for the second argument
std::function<double(double)> myDivBind2Nd= std::bind(divMe, 1.0, _1);
std::cout << "1/2.0= " << myDivBind2Nd(2.0) << std::endl;
std::cout << std::endl;
}
/*
1/2.0= 0.5
1/2.0= 0.5
1/2.0= 0.5
1/2.0= 0.5
1/2.0= 0.5
*/
std::bindμ νΉμ§μ λ€μκ³Ό κ°λ€.
- μΈμλ₯Ό μνλ μμΉμ λ°μΈλ©ν μ μλ€.
- μΈμμ μμλ₯Ό λ°κΏ μ μλ€.
- μΈμμ μ리 νμμ(placeholder)λ₯Ό μ μ©ν μ μλ€.
- ν¨μμ μΌλΆλΆλ§ νκ°ν μ μλ€.
- μλ‘ μμ±λ ν¨μ μ€λΈμ νΈλ₯Ό νΈμΆνκ³ std::functionμ μ μ₯ν μ μλ€.
std::functionμ μμμ μ½λ¬λΈ(ν¨μ ννμ νΈμΆμ)μ λ³μμ μ μ₯ν μ μλ€. μ½λ¬λΈμ μΌλ° ν¨μλΏλ§ μλλΌ λλ€ ν¨μ νΉμ ν¨μ μ€λΈμ νΈμΌ μλ μλ€.
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
int main(){
std::cout << std::endl;
// dispatch table
std::map< const char , std::function<double(double, double)> > dispTable;
dispTable.insert( std::make_pair('+', [](double a, double b){ return a + b;}));
dispTable.insert( std::make_pair('-', [](double a, double b){ return a - b;}));
dispTable.insert( std::make_pair('*', [](double a, double b){ return a * b;}));
dispTable.insert( std::make_pair('/', [](double a, double b){ return a / b;}));
// do the math
std::cout << "3.5 + 4.5= " << dispTable['+'](3.5, 4.5) << std::endl;
std::cout << "3.5 - 4.5= " << dispTable['-'](3.5, 4.5) << std::endl;
std::cout << "3.5 * 4.5= " << dispTable['*'](3.5, 4.5) << std::endl;
std::cout << "3.5 / 4.5= " << dispTable['/'](3.5, 4.5) << std::endl;
// add a new operation
dispTable.insert( std::make_pair('^', [](double a, double b){ return std::pow(a, b); }));
std::cout << "3.5 ^ 4.5= " << dispTable['^'](3.5, 4.5) << std::endl;
std::cout << std::endl;
};
/*
3.5 + 4.5= 8
3.5 - 4.5= -1
3.5 * 4.5= 15.75
3.5 / 4.5= 0.777778
3.5 ^ 4.5= 280.741
*/