accumulate
#include<iostream>
#include<string>
using namespace std;
#include<vector>
#include<algorithm>
#include<numeric>
/*accumulate
int sum = accumulate(v.begin(), v.end(), 0);
//参数列表:1:起始迭代器,2:结束迭代器,3:起始值
*/
class MyPrint {
public:
void operator()(int& a) {
cout << a << " ";
}
};
class Greater10 {
public:
bool operator()(int& a) {
return a > 5;
}
};
void test() {
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
v.push_back(6);
for_each(v.begin(), v.end(), MyPrint());cout << endl;
int sum = accumulate(v.begin(), v.end(), 0);
cout << sum << endl;
}
int main() {
test();
return 0;
}
fill
#include<iostream>
#include<string>
using namespace std;
#include<vector>
#include<algorithm>
#include<numeric>
/*fill
fill(v.begin(), v.end(), 100);
//参数列表:1:起始迭代器,2:结束迭代器,3:elem
*/
class MyPrint {
public:
void operator()(int& a) {
cout << a << " ";
}
};
class Greater10 {
public:
bool operator()(int& a) {
return a > 5;
}
};
void test() {
vector<int> v;
v.resize(10);
fill(v.begin(), v.end(), 100);
for_each(v.begin(), v.end(), MyPrint());cout << endl;
int sum = accumulate(v.begin(), v.end(), 0);
cout << sum << endl;
}
int main() {
test();
return 0;
}