//STL示例 仿函数
#include <iostream>
#include <numeric>
#include <vector>
#include <functional>
using namespace std;
#define MAX 10
vector<long> v(MAX);
int main()
{
//初始化容器元素
for (int i = 0; i < MAX; i++)
v[i] = i + 1;
// Accumulate函数对容器内元素求和,头文件numeric
long sum =accumulate(v.begin(), v.end(), 0);
cout << "Sum of values == " << sum << endl;
//函数模版accumulate利用multiplies仿函数(头文件functional)对容器内元素进行连续相乘操作
long product =accumulate(v.begin(), v.end(), 1, multiplies<long>());
cout << "Product of values == " << product << endl;
return 0;
}
STL示例06(仿函数multiplies)
最新推荐文章于 2022-01-09 16:32:01 发布