代码如下:
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}int total = accumulate(v.begin(), v.end(), 0);cout << "total = " << total << endl;
}int main()
{test01();return 0;
}
测试结果:
总结:
代码如下:
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;class myPrint
{
public:void operator()(int val){cout << val << " ";}
};void test01()
{vector<int>v;v.resize(10);fill(v.begin(), v.end(), 100);for_each(v.begin(), v.end(), myPrint());cout << endl;
}int main()
{test01();return 0;
}
测试结果:
总结: