您的位置:首页 > 编程语言 > C语言/C++

C++ templates chapter 6(Using Templates in Practice)

2009-09-24 22:26 483 查看
tracertest.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 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: