C++ templates chapter 6(Using Templates in Practice)_#includeC++ templates chapter 6(Using Templates in Practice)_学习_02tracer.hpp
 1 #include <iostream>
 2 using namespace std;
 3 
 4 class SortTracer
 5 {
 6 private:
 7     int value;
 8     int generation;
 9     static long n_created;
10     static long n_destroyed;
11     static long n_assigned;
12     static long n_compared;
13     static long n_max_live;
14 
15     static void update_max_live()
16     {
17         if(n_created-n_destroyed > n_max_live)
18         {
19             n_max_live = n_created-n_destroyed;
20         }
21     }
22 public:
23     static long creations()
24     {
25         return n_created;
26     }
27 
28     static long destructions()
29     {
30         return n_destroyed;
31     }
32 
33     static long assignments()
34     {
35         return n_compared;
36     }
37 
38     static long comparisons()
39     {
40         return n_compared;
41     }
42 
43     static long max_live()
44     {
45         return n_max_live;
46     }
47 
48 public:
49     SortTracer(int v=0) : value(v), generation(1)
50     {
51         ++n_created;
52         update_max_live();
53         cerr << "SortTracer #" << n_created << ", created generation " << generation << " (total: " << n_created - n_destroyed << ")\n";
54     }
55 
56     SortTracer(const SortTracer& b) : value(b.value), generation(b.generation+1)
57     {
58         ++n_created;
59         update_max_live();
60         cerr << "SortTracer #" << n_created << ", copied as generation " << generation << " (total: " << n_created - n_destroyed << ")\n";
61     }
62 
63     ~SortTracer()
64     {
65         ++n_destroyed;
66         update_max_live();
67         cerr << "SortTracer generation" << generation << " destroyed (total: " << n_created - n_destroyed << ")\n";
68     }
69 
70     SortTracer& operator=(const SortTracer& b)
71     {
72         ++n_assigned;
73         std::cerr << "SortTracer assignment #" << n_assigned 
74                   << " (generation " << generation 
75                   << " = " << b.generation 
76                   << ")\n"
77         value = b.value; 
78         return *this
79     } 
80 
81     friend bool operator < (SortTracer const& a, 
82                             SortTracer const& b) { 
83         ++n_compared; 
84         std::cerr << "SortTracer comparison #" << n_compared 
85                   << " (generation " << a.generation 
86                   << " < " << b.generation 
87                   << ")\n"
88         return a.value < b.value; 
89     } 
90 
91     int val() const { 
92         return value; 
93     } 
94     
95 };


C++ templates chapter 6(Using Templates in Practice)_#includeC++ templates chapter 6(Using Templates in Practice)_学习_02tracertest.cpp
 1 #include <iostream> 
 2 #include <algorithm> 
 3 #include "tracer.hpp" 
 4 using namespace std;
 5 
 6 long SortTracer::n_created = 0
 7 long SortTracer::n_destroyed = 0
 8 long SortTracer::n_max_live = 0
 9 long SortTracer::n_assigned = 0
10 long SortTracer::n_compared = 0
11 int main() 
12 
13     // prepare sample input: 
14     SortTracer input[]={7,3,5,6,4,2,0,1,9,8}; 
15 
16     // print initial values: 
17     for (int i=0; i<10++i) { 
18         cerr << input[i].val() << ' '
19     } 
20     cerr << endl; 
21 
22     // remember initial conditions: 
23     long created_at_start = SortTracer::creations(); 
24     long max_live_at_start = SortTracer::max_live(); 
25     long assigned_at_start = SortTracer::assignments(); 
26     long compared_at_start = SortTracer::comparisons(); 
27 
28     // execute algorithm: 
29     cerr << "---[ Start sort() ]--------------------\n"
30     sort<>(&input[0], &input[9]+1); 
31     cerr << "---[ End sort() ]----------------------\n"
32 
33     // verify result: 
34     for (int i=0; i<10++i) { 
35         cerr << input[i].val() << ' '
36     } 
37     cerr << "\n\n"
38 
39     // final report: 
40     cerr << "sort() of 10 SortTracer's" 
41               << " was performed by:\n " 
42               << SortTracer::creations() - created_at_start 
43               << " temporary tracers\n " 
44               << "up to " 
45               << SortTracer::max_live() 
46               << " tracers at the same time (" 
47               << max_live_at_start << " before)\n " 
48               << SortTracer::assignments() - assigned_at_start 
49               << " assignments\n " 
50               << SortTracer::comparisons() - compared_at_start 
51               << " comparisons\n\n"
52 
53     cin.get();
54