bind1st和bind2nd函數把一個二進制函數對象綁定成為一個一進制函數對象。但是由于二進制函數對象接受兩個參數,在綁定成為一進制函數對象時需要将原來兩個參數中的一個綁定下來。也即通過綁定二進制函數對象的一個參數使之成為一進制函數對象的。bind1st是綁定第一個參數,bind2nd則是綁定第二個參數。
bind1st(const Fn2& Func,const Ty& left);
bind2nd(const Fn2& Func,const Ty& right);
//例子1:
//coll:1 2 3 4 5 6 7 8 9 foPow:是std::pow的一個仿函數
transform(coll.begin(),coll.end(),ostream_iterator<int>(cout,""),
bind1st(foPow<float,int>(),3))
結果:3 9 27 81 243 729 2187 6561 19683
解釋:把參數3放在左邊,即3的幾次方。
transform(coll.begin(),coll.end(),ostream_iterator<int>(cout,""),
bind2nd(foPow<float,int>(),3))
結果: 1 8 27 64 125 216 343 512 729
解釋:把參數3放在右邊,即數的3次方。
//例子2:
// 移除所有大于100的元素
arr.erase( std::remove_if( arr.begin(), arr.end(),
std::bind1st( std::less< int>(), 100)), arr.end());
解釋:把參數100放在左邊,即100<。
// 移除所有小于100的元素
arr.erase( std::remove_if( arr.begin(), arr.end(),
std::bind2nd( std::less< int>(), 100)), arr.end());
解釋:把參數100放在右邊,即<100。