天津做网站建设公司,吃什么补肾最快最好,wordpress语言的设置中文,wordpress语言设置若一个类重载了运算符“#xff08;#xff09;”#xff0c;则该类的对象就成为函数对象。函数对象可以用于标准库算法。函数对象和函数指针很相似#xff0c;但也有区别。当函数对象使用模板时可以赋值给函数指针。 #include iostream
#include vector
#incl…若一个类重载了运算符“”则该类的对象就成为函数对象。函数对象可以用于标准库算法。函数对象和函数指针很相似但也有区别。当函数对象使用模板时可以赋值给函数指针。 #include iostream
#include vector
#include algorithm
#include numeric
using namespace std;int sumsquares(int total, int value)
{return (total value*value);
}template class T
void printValue(T first, T last)
{for (; first ! last; first){cout *(first) ;}cout endl;
}template class T
class sumPower
{
private:int power;
public:sumPower(int power) :power(power) {};const T operator()(const T total, const T value){T v value;cout the function is called endl;for (int i 1; i power; i)v * value;return (total v);}
};int main()
{const int size 3;int a[] { 1,2,3 };vectorint a1(a, a size);printValue(a1.begin(),a1.end());int result accumulate(a1.begin(), a1.end(), 0,sumsquares);cout 1) 平方和 result endl;sumPowerint b(4);//每次调用它的函数形参时它都使用相应的调用操作符result accumulate(a1.begin(), a1.end(), 0, sumPowerint(3));cout 2) 立方和 result endl;return 0;
} 函数运行结果 参考链接 https://www.coursera.org/learn/cpp-chengxu-sheji转载于:https://www.cnblogs.com/helloforworld/p/5655378.html