问题及代码:
/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:zwj.cpp
*作 者:张伟晶
*完成日期:2016年6月3日
*版 本 号:v1.0
*
*问题描述:阅读程序,对照运行结果,领会STL的用法
*输入描述:
*程序输出:
*/
#include <functional>
#include <numeric>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int> v(5);
for (int i = 0; i < 5; i++)
v[i] = i + 1;
int sum = accumulate(v.begin(), v.end(), 0);
cout << "Sum of values == " << sum << endl;
int product = accumulate(v.begin(), v.end(),1, multiplies<long>());
cout << "Product of values == " << product << endl;
return 0;
}
运行结果:
知识点总结:
STL函数对象 | 类型 | 功能说明 |
plus<T> | 算术 | 输入两个类型为T的操作数x,y,返回x+y |
minus<T> | 算术 | 输入两个类型为T的操作数x、y,返回x-y |
multiplies<T> | 算术 | 输入两个类型为T的操作数x、y,返回x*y |
divides<T> | 算术 | 输入两个类型为T的操作数x、y,返回x/y |
modulus<T> | 算术 | 输入两个类型为T的操作数x、y,返回x%y的结果 |
negate<T> | 算术 | 输入一个类型为T的操作数x,返回-x |