Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
for_each.cpp
Go to the documentation of this file.
1
30#include <algorithm> // for for_each
31#include <functional> // for less, unary_function
32#include <iostream> // for cout, endl
33#include <set> // for set
34
35#include <examples.h>
36
37
38// Function object that outputs its argument times x.
39template <class Arg>
40class out_times_x: public std::unary_function<Arg, void>
41{
42 Arg multiplier;
43public:
44 out_times_x (const Arg &x) : multiplier (x) { }
45
46 void operator() (const Arg &x) const {
47 std::cout << x * multiplier << " " << std::endl;
48 }
49};
50
51
52int main ()
53{
54 // Typedef for convenience.
55 typedef std::set<int, std::less<int>, std::allocator<int> > sequence;
56
57 sequence::value_type arr [] = { 1, 2, 3, 4, 5 };
58
59 // Populate a sequence from arr.
60 sequence s (arr, arr + sizeof arr / sizeof *arr);
61
62 // Construct a function object.
64
65 // Apply function object's operator() to each element.
66 std::for_each (s.begin (), s.end (), f2);
67
68 return 0;
69}
#define x
Definition 17.names.cpp:140
#define Arg
Definition 17.names.cpp:442
#define f2
Definition 17.names.cpp:256
#define s
out_times_x(const Arg &x)
Definition for_each.cpp:44
void operator()(const Arg &x) const
Definition for_each.cpp:46
int main()
Definition for_each.cpp:52