When Gauss was ten,he calculated 1+2+3+4+…..+99+100 like this:(1+100)*100/2 =5050
Now in my twentiesI calculated it by Using C++ as below:
#include "iostream"
#include "string.h"
#include <vector>
#include <algorithm>
using namespace std;
template<class T> class n_Sum;
template<class T> class n_Sum_Implt
{
T sum;
friend class n_Sum <T>;
public:
n_Sum_Implt(T i=0):sum(i)
{
}
void operator()(const T& value)
{
sum += value;
}
};
template<class T> class n_Sum
{
T sum;
n_Sum_Implt<T>* pn_Sum_Implt;
public:
n_Sum(T i=0):sum(i)
{
}
void SetImplentPtr(n_Sum_Implt<T>* pImplt)
{
pn_Sum_Implt = pImplt;
}
void operator()(const T& x)
{
pn_Sum_Implt->operator() (x);
}
T result() const
{
return pn_Sum_Implt->sum;
}
};
int main()
{
int i;
vector<int> vi;
n_Sum<int> sum;
n_Sum_Implt<int> sumImplt;
sum.SetImplentPtr(&sumImplt);
for(i=1; i<=100; i++)
{
vi.push_back(i);
}
for_each(vi.begin(), vi.end(), sum);
cout<<"result:"<<sum.result()<<endl;
return 0;
}
分享到:
相关推荐
c++实现计算器编写,可实现普通简单运算和复杂运算
calculate.cpp
Described how to calculate the Band Structure using VASP, taking Si as an example.
Example: A Function to Calculate the Scalar Product of Two Vectors . . . . . . . . . . . . . . . . . . . . . . . Reference Variables . . . . . . . . . . . . . . . . . . . . . . . Default Values for ...
to calculate trading total earn coin by usdt
基于MFC的对话框程序,计算器小程序,对各个控件的使用,有很好的了解。能很好的了解,消息响应,和消息映射。对新手很有用!!
code c++ calculate the features using lbp
Calculate1.m
光流法C++源代码
used to calculate a plus
Sample07: How to access pixels data using the C++ interface of OpenCV. Sample08: How to apply MorphologyEx filters to images. Sample09: How to resize, rotate and blur images. Sample10: How to ...
Sample07: How to access pixels data using the C++ interface of OpenCV. Sample08: How to apply MorphologyEx filters to images. Sample09: How to resize, rotate and blur images. Sample10: How to ...
Using R for Statistics will get you the answers to most of the problems you are likely to encounter when using a variety of statistics. This book is a problem-solution primer for using R to set up ...
c语言 微积分 本人自写 可以参考 用英语写的哟 希望和诸位多交流~
Sample07: How to access pixels data using the C++ interface of OpenCV. Sample08: How to apply MorphologyEx filters to images. Sample09: How to resize, rotate and blur images. Sample10: How to ...
MATLAB code to calculate the 3D skeleton of a binary volume using parallel medial axis thinning.zip
Provide member function calculateMonthlyInterest that calculates the monthly interest by multiplying the balance by annualInterestRate divided by 12; this interest should be added to savingsBalance. ...
matlab开发-Calculatesize。calculateSize计算用于游戏cody的matlab文件的大小
1 NetworkCalculus 2 ApplicationtotheInternet II MathematicalBackground 3 BasicMin-plusandMax-plusCalculus 4 Min-plusandMax-PlusSystemTheory III ASecondCourseinNetworkCalculus 5 ...
std::string src = argc > 1 ? argv[1] : "12+((2+73)*4)-15"; std::cout ; Expression expression; Expression::PrefixType result; int ret = expression.ToPrefix(src, result); if (ret != 0) { std::...