The concept of function symbols :
- A generator is a function symbol that can be called without parameters
- Unary function is a function symbol that can be called with one parameter
- The second hospital function uses two parameters to call the function symbol
- The unary function operator that returns a bool value is a predicate
- The binary function operator that returns the bool value is a binary predicate
Use bind1st or bind2nd function to convert one or two parameters into a single parameter function symbol
1. The use of transform ()
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <cmath>
#include <functional> /*定义了多个模板类函数对象*/
double mean(double x, double y)
{
return x + y;
}
int main()
{
using namespace std;
const int LIM = 5;
double arr1[LIM] = { 36,39,42,45,48 };
vector<double>ar8(arr1, arr1 + LIM);
ostream_iterator<double, char>out(cout, " ");
vector<double>a = { 33,69,18,38,96 };
transform(ar8.begin(), ar8.end(), a.begin(),out, mean); /*这时候ar8的区间长度必须与a的长度一致*/
cout << endl;
plus<double>add;/*头文件functional定义的 来完成常规相加运算*/
transform(ar8.begin(), ar8.end(), a.begin(), out, add);
cout << endl;
transform(ar8.begin(), ar8.end(), out, bind1st(multiplies<double>(), 2.5));/*1st只是将第一个参数是常数赋值,第二个接受一个*/
system("pause");
return 0;
}