Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
memfunc.cpp
Go to the documentation of this file.
1
30#if defined (__IBMCPP__)
31# include <rw/_config.h>
32 // disable implicit inclusion to work around
33 // a limitation in IBM VisualAge 5 (PR #26959)
34# ifndef _RWSTD_NO_IMPLICIT_INCLUSION
35# define _RWSTD_NO_IMPLICIT_INCLUSION
36# endif // _RWSTD_NO_IMPLICIT_INCLUSION
37#endif // __IBMCPP__
38
39#include <algorithm> // for for_each, stable_sort
40#include <cstddef> // for size_t
41#include <functional> // for greater
42#include <iomanip> // for setprecision, setw
43#include <ios> // for fixed, left, right
44#include <iostream> // for cout
45#include <vector> // for vector
46
47#include <examples.h>
48
49
50// Very large city class
52{
53public:
54
55 Megapolis (const char *s = "", double n = 0)
56 :population(n), cityName(s) { }
57
58 // The following function cannot return void due to limitations in
59 // some current C++ implementations.
60 virtual std::size_t byPopulation () const {
61 std::cout << std::setw (12) << std::left << cityName
62 << std::setw (12) << std::left << "Megapolis"
63 << std::setw (5) << std::right
64 << std::fixed << std::setprecision (1)
65 << population << '\n';
66 return 0;
67 }
68
69 bool operator< ( const Megapolis* ptr_ ) {
70 return population > ptr_->population;
71 }
72
73 double population;
74
75protected:
76
77 const char* cityName;
78};
79
80
81// City and surrounding area class
82struct Metropolis: public Megapolis
83{
84 Metropolis (const char *s = "", double n = 0)
85 : Megapolis (s, n){ }
86
87 virtual std::size_t byPopulation () const {
88 std::cout << std::setw (12) << std::left << cityName
89 << std::setw (12) << std::left << "Metropolis"
90 << std::setw (5) << std::right
91 << std::fixed << std::setprecision (1)
92 << population << '\n';
93 return 0;
94 }
95};
96
97
98namespace std {
99
100// specialize the standard function object for Megapolis*
101template<>
102struct greater<Megapolis*>
103{
106 typedef bool result_type;
107
110 return y->population < x->population;
111 }
112};
113
114} // namespace std
115
116
117int main ()
118{
119 // Create a vector of very large cities
120 std::vector<Megapolis*, std::allocator<Megapolis*> > cities;
121
122 cities.push_back (new Megapolis ("Calcutta", 12));
123 cities.push_back (new Megapolis ("Tokyo", 26.8));
124 cities.push_back (new Megapolis ("Delhi", 10));
125 cities.push_back (new Megapolis ("Bombay", 15));
126
127 cities.push_back (new Metropolis ("Cairo", 12));
128 cities.push_back (new Metropolis ("New York", 7.5));
129 cities.push_back (new Metropolis ("Los Angeles", 3.7));
130 cities.push_back (new Metropolis ("Jakarta", 8.2));
131
132 // Now use mem_fun to pass byPopulation member function
133 // of Megapolis to the for_each function.
134 std::cout << "City Type Population (millions)\n";
135 std::cout << "----------- ----------- ---------------------\n";
136
137 std::for_each (cities.begin(), cities.end(),
138 std::mem_fun (&Megapolis::byPopulation));
139
140 std::cout << "\nAfter sorting...\n\n";
141
142 // Sort
143 std::stable_sort (cities.begin (), cities.end (),
144 std::greater<Megapolis*>());
145
146 std::for_each (cities.begin (), cities.end (),
147 std::mem_fun (&Megapolis::byPopulation));
148
149 // Free allocated memory
150 while (!cities.empty ()) {
151 delete cities.back ();
152 cities.pop_back ();
153 }
154
155 return 0;
156}
#define y
Definition 17.names.cpp:141
#define x
Definition 17.names.cpp:140
#define s
#define n
Definition 17.names.cpp:130
bool operator<(const Megapolis *ptr_)
Definition memfunc.cpp:69
double population
Definition memfunc.cpp:73
const char * cityName
Definition memfunc.cpp:77
Megapolis(const char *s="", double n=0)
Definition memfunc.cpp:55
virtual std::size_t byPopulation() const
Definition memfunc.cpp:60
int main()
Definition memfunc.cpp:117
Definition json.h:5363
Metropolis(const char *s="", double n=0)
Definition memfunc.cpp:84
virtual std::size_t byPopulation() const
Definition memfunc.cpp:87
const Megapolis * first_argument_type
Definition memfunc.cpp:104
const Megapolis * second_argument_type
Definition memfunc.cpp:105