Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
Main Page
Related Pages
Topics
Namespaces
Data Structures
Files
File List
Globals
EXTERNAL_HEADERS
RSL
examples
manual
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.
39
template
<
class
Arg>
40
class
out_times_x
:
public
std::unary_function<Arg, void>
41
{
42
Arg
multiplier;
43
public
:
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
52
int
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.
63
out_times_x<sequence::value_type>
f2
(2);
64
65
// Apply function object's operator() to each element.
66
std::for_each (
s
.begin (),
s
.end (),
f2
);
67
68
return
0;
69
}
x
#define x
Definition
17.names.cpp:140
Arg
#define Arg
Definition
17.names.cpp:442
f2
#define f2
Definition
17.names.cpp:256
s
#define s
Definition
rw_alg_test.h:221
out_times_x
Definition
for_each.cpp:41
out_times_x::out_times_x
out_times_x(const Arg &x)
Definition
for_each.cpp:44
out_times_x::operator()
void operator()(const Arg &x) const
Definition
for_each.cpp:46
examples.h
main
int main()
Definition
for_each.cpp:52
Generated by
1.9.8