二元函数对象,如果返回值的是bool,那就叫做二元谓词
#include <iostream>//二元函数对象,如果返回值的是bool,那就叫做二元谓词
#include <algorithm>
#include <vector>
using namespace std;
template<typename elementType>
class CMultiply
{
public:
elementType operator() (const elementType& elem1, const elementType& elem2)//二元函数作参数
{
return elem1*elem2;
}
};
int main()
{
vector<int> a, b;
for (int i = 0; i < 10; ++i)
a.push_back(i);
for (int j = 100; j < 110; ++j)
b.push_back(j);
vector<int> vecResult;
vecResult.resize(10);
//transform变换算法
transform(a.begin(), a.end(), b.begin(), vecResult.begin(), CMultiply<int>());
for (size_t nIndex = 0; nIndex < vecResult.size(); ++nIndex)
cout << vecResult[nIndex] << ' ';
cout << endl;
cout << "hello二元函数对象" << endl;
getchar();
return 0;
}
来源:CSDN
作者:txwtech
链接:https://blog.csdn.net/txwtech/article/details/103751963