accumulate
accumulate(iterator beg, iterator end, value);
value: 起始累加值
title:Important
collapse:open
#include <iostream>
#include <numeric>
using namespace std;
void print(vector<int>& v)
{
for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
vector<int>v1;
for(int i = 0; i < 10; i++)
{
v1.push_back(i);
}
print(v1);
int total = accumulate(v1.begin(), v1.end(), 0);
cout << total << endl;
return 0;
}
/*output:
0 1 2 3 4 5 6 7 8 9
55
*/
fill
fill(iterator beg, iterator end, value);
title:Example
collapse:open
#include <iostream>
#include <numeric>
using namespace std;
void print(vector<int>& v)
{
for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
vector<int>v1;
v1.resize(10);
fill(v1.begin(), v1.end(), 100);
print(v1);
return 0;
}
/*output:
100 100 100 100 100 100 100 100 100 100
*/