cc23c_demo-23_21days_Cpp_函数对象c++ 调用操作符的重载与函数对象-二元函数对象-代码示范

二元函数对象,如果返回值的是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;
}
发布了356 篇原创文章 · 获赞 186 · 访问量 89万+

猜你喜欢

转载自blog.csdn.net/txwtech/article/details/103751963