Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
funct_ob.cpp
Go to the documentation of this file.
1
29#include <algorithm>
30#include <deque>
31#include <functional>
32#include <iostream>
33#include <iterator>
34#include <vector>
35
36
37// Create a new function object from unary_function.
38template <class Arg, class Result>
39struct factorial: public std::unary_function<Arg, Result>
40{
42 Result a = 1;
43 for (Arg i = 2; i <= arg; i++)
44 a *= i;
45 return a;
46 }
47};
48
49
50int main ()
51{
52 // Typedefs for convenience.
53 typedef std::deque<int, std::allocator<int> > Deque;
54 typedef std::vector<long, std::allocator<long> > Vector;
55
56 // Initialize a deque with an array of integers.
57 Deque::value_type arr[] = { 1, 2, 3, 4, 5, 6, 7 };
58 Deque d (arr, arr + sizeof arr / sizeof *arr);
59
60 // Create an empty vector to store the factorials.
61 Vector v;
62
63 // Transform the numbers in the deque to their factorials
64 // and store in the vector.
65 std::transform (d.begin (), d.end (),
66 std::back_inserter (v),
68
69 // Create an iterator to output deque elements.
70 std::ostream_iterator<Deque::value_type,
71 char,
72 std::char_traits<char> > outd (std::cout, " ");
73
74 // Print the results.
75 std::cout << "The following numbers: \n ";
76 std::copy (d.begin (), d.end (), outd);
77
78 // Create an iterator to output vector elements.
79 std::ostream_iterator<Vector::value_type,
80 char,
81 std::char_traits<char> > outv (std::cout, " ");
82
83 std::cout << std::endl;
84 std::cout << "\nHave the factorials: \n ";
85 std::copy (v.begin (), v.end (), outv);
86 std::cout << std::endl;
87
88 return 0;
89}
#define i
Definition 17.names.cpp:126
#define d
Definition 17.names.cpp:115
#define v
Definition 17.names.cpp:138
#define Result
Definition 17.names.cpp:530
#define a
Definition 17.names.cpp:109
#define Arg
Definition 17.names.cpp:442
std::vector< UserClass, std::allocator< UserClass > > Vector
std::deque< std::string, std::allocator< std::string > > Deque
Definition deque.cpp:35
int main()
Definition funct_ob.cpp:50
Result operator()(const Arg &arg)
Definition funct_ob.cpp:41
int arg
Definition locale.cpp:2806