for_each
#include <algorithm>
#include <iostream>
#include <vector>
using std::vector,
std::cerr;
class Temp{
public:
int a, b;
Temp(int i, int j):a(i), b(j){}
};
typedef vector<Temp> my_vector;
int main(){
my_vector a = { Temp(2, 4), Temp(3, 9), Temp(4, 16), Temp(5, 25)};
auto inc = [](Temp &t){
t.a +=1;
t.b *=2;
};
auto print = [](Temp t){
cerr << "Temp(" << t.a << ", " << t.b << ")\n";
};
cerr << "before inc:\n";
for_each(a.begin(), a.end(), print);
for_each(a.begin(), a.end(), inc);
cerr << "after inc:\n";
for_each(a.begin(), a.end(), print);
return 0;
}
back
C++ Examples© 2024 TBD