今天总算是弄明白bind1st和bind2nd是怎么用的,有什么区别了。只知道工作,不知道学习很久了。
// bind1st example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
int main () {
int numbers[] = {10,20,30,40,50,10};
int cx;
cx = count_if (numbers, numbers+6, bind1st(greater<int>(),10) );
cout << "There are " << cx << " elements that are equal to 10.\n";
return 0;
}
// bind2nd example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
int main () {
int numbers[] = {10,20,30,40,50,10};
int cx;
cx = count_if (numbers, numbers+6, bind2nd(greater<int>(),10) );
cout << "There are " << cx << " elements that are equal to 10.\n";
return 0;
}
Reference:
http://stackoverflow.com/questions/6112573/bind1st-and-bind2nd
http://stackoverflow.com/questions/6863677/use-bind1st-or-bind2nd
本文详细介绍了bind1st和bind2nd在C++中的使用方法,通过具体示例展示了它们如何在算法操作中提高代码的灵活性和效率。重点讨论了它们之间的区别及应用场景。
1334

被折叠的 条评论
为什么被折叠?



