C++之算术生成算法
accumulate
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
void test()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
int total = accumulate(v.begin(), v.end(),0);
cout << total << endl;
}
int main()
{
test();
system("pause");
return 0;
}
fill
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
class MyPrint
{
public:
void operator()(int val)
{
cout << val<<" ";
}
};
void test()
{
vector<int> v;
v.resize(10);
cout << "默认值" << endl;
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
fill(v.begin(), v.end(), 100);
cout << "从新填充" << endl;
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
}
int main()
{
test();
system("pause");
return 0;
}