• C++模板编程(7)---实际运用模板:模板追踪器(tracer)


    我们知道即使程序编译链接成功,也不能保证程序运行正常。template有时候会加深这个问题,因为每个template表述的泛型程序代码取决于实例化类型。追踪器tracer是一种软件装置,有助于在开发初期检测模板定义式的问题,减轻后续排错工作的负担。

       本文从一个实际的排序算法的跟踪器为例,用来测试标准库中的排序算法:

    1. 新建一个QT控制台工程,添加class SortTracer,新建后工程如下图,

    2. //main.cpp 内容

    #include <QCoreApplication>
     
    
    #include <iostream>
    #include <algorithm>
    #include "sorttracer.h"
     
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
     
    
        // prepare sample input:
        SortTracer input[] = { 7, 3, 5, 6, 4, 2, 0, 1, 9, 8 };
     
    
        // print initial values:
        for (int i=0; i<10; ++i) {
            std::cerr << input[i].val() << ' ';
        }
        std::cerr << std::endl;
     
    
        // remember initial conditions:
        long created_at_start = SortTracer::creations();
        long max_live_at_start = SortTracer::max_live();
        long assigned_at_start = SortTracer::assignments();
        long compared_at_start = SortTracer::comparisons();
     
    
        // execute algorithm:
        std::cerr << "---[ Start std::sort() ]--------------------\n";
        std::sort<>(&input[0], &input[9]+1);
        std::cerr << "---[ End std::sort() ]----------------------\n";
     
    
        // verify result:
        for (int i=0; i<10; ++i) {
            std::cerr << input[i].val() << ' ';
        }
        std::cerr << "\n\n";
     
    
        // final report:
        std::cerr << "std::sort() of 10 SortTracer's"
                  << " was performed by:\n "
                  << SortTracer::creations() - created_at_start
                  << " temporary tracers\n "
                  << "up to "
                  << SortTracer::max_live()
                  << " tracers at the same time ("
                  << max_live_at_start << " before)\n "
                  << SortTracer::assignments() - assigned_at_start
                  << " assignments\n "
                  << SortTracer::comparisons() - compared_at_start
                  << " comparisons\n\n";
     
    
        return a.exec();
    }

    3.//sorttracer.h内容

    #ifndef SORTTRACER_H
    #define SORTTRACER_H
     
    
    #include <iostream>
     
    
    class SortTracer {
      private:
        int value;                // integer value to be sorted
        int generation;           // generation of this tracer
        static long n_created;    // number of constructor calls
        static long n_destroyed;  // number of destructor calls
        static long n_assigned;   // number of assignments
        static long n_compared;   // number of comparisons
        static long n_max_live;   // maximum of existing objects
     
    
        // recompute maximum of existing objects
        static void update_max_live() {
            if (n_created-n_destroyed > n_max_live) {
                n_max_live = n_created-n_destroyed;
            }
        }
     
    
      public:
        static long creations() {
            return n_created;
        }
        static long destructions() {
            return n_destroyed;
        }
        static long assignments() {
            return n_assigned;
        }
        static long comparisons() {
            return n_compared;
        }
        static long max_live() {
            return n_max_live;
        }
     
    
      public:
        // constructor
        SortTracer (int v = 0) : value(v), generation(1) {
            ++n_created;
            update_max_live();
            std::cerr << "SortTracer #" << n_created
                      << ", created generation " << generation
                      << " (total: " << n_created - n_destroyed
                      << ")\n";
        }
     
    
        // copy constructor
        SortTracer (SortTracer const& b)
         : value(b.value), generation(b.generation+1) {
            ++n_created;
            update_max_live();
            std::cerr << "SortTracer #" << n_created
                      << ", copied as generation " << generation
                      << " (total: " << n_created - n_destroyed
                      << ")\n";
        }
     
    
        // destructor
        ~SortTracer() {
            ++n_destroyed;
            update_max_live();
            std::cerr << "SortTracer generation " << generation
                      << " destroyed (total: "
                      << n_created - n_destroyed << ")\n";
        }
     
    
        // assignment
        SortTracer& operator= (SortTracer const& b) {
            ++n_assigned;
            std::cerr << "SortTracer assignment #" << n_assigned
                      << " (generation " << generation
                      << " = " << b.generation
                      << ")\n";
            value = b.value;
            return *this;
        }
     
    
        // comparison
        friend bool operator < (SortTracer const& a,
                                SortTracer const& b) {
            ++n_compared;
            std::cerr << "SortTracer comparison #" << n_compared
                      << " (generation " << a.generation
                      << " < " << b.generation
                      << ")\n";
            return a.value < b.value;
        }
     
    
        int val() const {
            return value;
        }
    };
     
    
    #endif // SORTTRACER_H
     
    

    4.//sorttracer.cpp内容

    #include "sorttracer.h"
     
    
    long SortTracer::n_created = 0;
    long SortTracer::n_destroyed = 0;
    long SortTracer::n_max_live = 0;
    long SortTracer::n_assigned = 0;
    long SortTracer::n_compared = 0;

  • 相关阅读:
    自学白帽子黑客第三年总结
    AJAX入门Day01笔记
    Ruby语言基础知识
    Java 最常见的800道面试题,老话说:临阵磨枪,不快也光,涨薪指日可待
    (附源码)计算机毕业设计Java“华商转转”平台的设计和实现
    2022-9-4QT实现迷你闹铃(定时播报指定内容)
    关于ORVIBO的HomeAI V4.0的分析
    ai批量剪辑矩阵无人直播一站式托管系统源头技术开发
    深度学习实战(2):肺炎预测|附数据集与源码
    【底层服务/编程功底系列】「网络通信体系」深入探索和分析TCP协议的运输连接管理的核心原理和技术要点
  • 原文地址:https://blog.csdn.net/zkmrobot/article/details/125435794