4.12
Copied!
#include <algorithm>
#include <iostream>
#include <list>
using namespace std;
class EvenOddFunctor {
int even_;
int odd_;
public:
EvenOddFunctor() : even_(0), odd_(0) {}
void operator()(int x) {
if (x%2 == 0) even_ = x;
else odd_ = x;
}
int even_sum() const { return even_; }
int odd_sum() const { return odd_; }
};
int main() {
EvenOddFunctor evenodd;
int my_list[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
evenodd = for_each(my_list,
my_list sizeof(my_list)/sizeof(my_list[0]),
evenodd);
cout << " Juftlar yigʻindisi: " << evenodd.even_sum() << endl;
cout << " Toqtlar yigʻindisi: " << evenodd.odd_sum() << endl;
system("pause");
return 0;
}
Run on cpp.sh
Copied!