| René Nyffenegger's collection of things on the web | |
|
René Nyffenegger on Oracle - Most wanted - Feedback
- Follow @renenyffenegger
|
c plus plus code fragments | ||
Calling a method for each element in a container
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
using namespace std;
struct A {
A(int a) : _a(a) {};
void g () { cout << "void f(), " << _a << endl;};
void f (int i) const { cout << "int A::f (" << i << "), " << _a << endl; }
int _a;
};
int main(int argc, char* argv[]) {
vector <A> v;
v.push_back (A (1));
v.push_back (A (1));
v.push_back (A (2));
v.push_back (A (3));
v.push_back (A (5));
v.push_back (A (8));
for_each (v.begin (), v.end (), mem_fun_ref(&A::g));
for_each (v.begin (), v.end (),
bind2nd (mem_fun_ref(&A::f), 10));
return 0;
}
forward declaration of a template
template <class T> class F;
template <class T>
void g(F<T>*);
struct G {
int i_;
};
template <class T> class F {
public:
T t_;
};
int main() {
F<G> f;
f.t_.i_=42;
g(&f);
}
template <class T>
void g(F<T>* f) {
}
|