First page Back Continue Last page Graphics
3. A function object is usually faster than a function pointer.
3. A function object is usually faster than a function pointer.
- 1. Example. Suppose you want to add a certain value to all elements of a collection. If you know the value you want to add at compile time, you could use an ordinary function:
- void add10 (int& elem)
- {
- elem += 10;
- }
- void f1( )
- {
- vector<int> coll;
- ...
- for_each(coll.begin( ), coll.end( ), add10)
- }