zoukankan      html  css  js  c++  java
  • C++调试函数的编写

    ACM线下赛中,很多时候并不提供很强大的调试软件,这样,如果要想调试查看map或list等信息,需要自行编写输出中间结果。

    这里提供一个通用框架,并附上示例,可以作为模板使用,提交代码时注释掉#define YLOFI和#define YDELO即可。

      1 #define YLOFI
      2 #define YDELO
      3 
      4 #include<iostream>
      5 #include<iomanip>
      6 #include<cstdio>
      7 #include<string>
      8 #include<sstream>
      9 #include<map>
     10 #include<list>
     11 #include<algorithm>
     12 using namespace std;
     13 #define YCOL1 10
     14 struct yc2d{
     15     int a;
     16     int b;
     17 };
     18 
     19 #ifdef YDELO
     20 //#include "YLog.h"
     21 #include "assert.h"
     22 int ydelon = 0;
     23 int ydelom = 0;
     24 //自定义类
     25 ostream &operator<<(ostream &os,const yc2d &myclass){
     26     return os << myclass.a << " " << myclass.b;
     27 }
     28 //二维数组
     29 template<typename T>
     30 void yPrintArr(const T x[][YCOL1]){
     31     int i = 0;
     32     while(1){
     33         cout << i;
     34         for(int j = 0;j<ydelom;j++){
     35             cout << " (" << j << "," << x[i][j] << ")";
     36         }
     37         i++;
     38         if(i >= ydelon){
     39             break;
     40         }
     41         else{
     42             cout << endl;
     43         }
     44     }
     45     return;
     46 }
     47 template<typename T>
     48 bool yPrint(const string &info,const T x[][YCOL1],int n = 0,int m = 0,bool clr = true){
     49     if(clr){
     50         system("cls");
     51     }
     52     cout << endl << "\**********************" << endl << info << endl;
     53     ydelon = n;
     54     ydelom = m;
     55     if(ydelon > 0 && ydelom > 0){
     56         yPrintArr(x);
     57     }
     58     else{
     59         return false;
     60     }
     61     cout << endl << "**********************\" << endl;
     62     return true;
     63 }
     64 //数组
     65 template<typename T,int size>
     66 void yPrintArr(const T (&x)[size]){
     67     int i = 0;
     68     while(1){
     69         cout << i << " " << x[i];
     70         i++;
     71         if(i >= ydelon){
     72             break;
     73         }
     74         else{
     75             cout << endl;
     76         }
     77     }
     78     return;
     79 }
     80 template<typename T,int size>
     81 bool yPrint(const string &info,const T (&x)[size],int n = 0,bool clr = true){
     82     if(clr){
     83         system("cls");
     84     }
     85     cout << endl << "\**********************" << endl << info << endl;
     86     ydelon = n;
     87     if(ydelon > 0){
     88         yPrintArr(x);
     89     }
     90     else{
     91         return false;
     92     }
     93     cout << endl << "**********************\" << endl;
     94     return true;
     95 }
     96 //非数组
     97 template<typename T>
     98 bool yPrint(const string &info,const T &x,int n = 0,bool clr = true){
     99     if(clr){
    100         system("cls");
    101     }
    102     cout << endl << "\**********************" << endl << info << endl;
    103     ydelon = n;
    104     if(ydelon >= 0){
    105         cout << x;
    106     }
    107     else{
    108         return false;
    109     }
    110     cout << endl << "**********************\" << endl;
    111     return true;
    112 }
    113 //list & map
    114 template<typename T,typename S>
    115 ostream &operator<<(ostream &os,const pair<T,S> &it){
    116     return     os << it.first << " " << it.second;
    117 }
    118 template<typename T,typename S>
    119 ostream &operator<<(ostream &os,const map<T,S> &st){
    120     int n = ydelon==0?st.size():ydelon,i = 0;
    121     os <<  " size=" << st.size() << " show=" << n << endl;
    122     for(typename map<T,S>::const_iterator it = st.begin();it!=st.end();it++){
    123         os << i << " " << *it;
    124         i++;
    125         if(i >= n){
    126             break;
    127         }
    128         else{    
    129             os << endl;
    130         }
    131     }
    132     return os;
    133 }
    134 template<typename T>
    135 ostream &operator<<(ostream &os,const list<T> &st){
    136     int n = ydelon==0?st.size():ydelon,i = 0;
    137     os <<  " size=" << st.size() << " show=" << n << endl;
    138     for(typename list<T>::const_iterator it = st.begin();it!=st.end();it++){
    139         os << i << " " << *it;
    140         i++;
    141         if(i >= n){
    142             break;
    143         }
    144         else{
    145             os << endl;
    146         }
    147     }
    148     return os;
    149 }
    150 #endif
    151 
    152 int main(){
    153     #ifdef YLOFI
    154     freopen("yin.txt","r",stdin);
    155     //freopen("yout.txt","w",stdout);
    156     #endif
    157     #ifdef YDELO
    158     assert(1);
    159     #endif
    160     map<int,int> ma;
    161     ma[1] = 9;
    162     ma[2] = 8;
    163     ma[-1] = 7;
    164     list<int> li;
    165     li.push_back(33);
    166     li.push_back(3);
    167     int a = 3;
    168     double f[10] = {1.1,2.2,3.3,4.4};
    169     yc2d myclass;
    170     myclass.a = 3;
    171     myclass.b = 2;
    172     int arr2[4][YCOL1] = {{0,1,2},{2,1,0}};
    173     cout << "here could be clear." << endl;
    174 #ifdef YDELO
    175     assert(yPrint("ma1",ma,2));
    176     assert(yPrint("li",li,0,false));
    177     assert(yPrint("a",a,0,false));
    178     assert(yPrint("f",f,4,false));
    179     assert(yPrint("myclass",myclass,0,false));
    180     assert(yPrint("arr2",arr2,2,3,false));
    181 #endif
    182     return 0;
    183 }

  • 相关阅读:
    LeetCode: 18. 4Sum
    LeetCode:15. 3Sum
    Leetcode:1. Two Sum
    tensorflow placeholder
    Tensorflow变量
    13.git的简单使用
    13.Django1.11.6文档
    12.python进程协程异步IO
    12.Flask-Restful
    12.Django思维导图
  • 原文地址:https://www.cnblogs.com/ywsswy/p/7856739.html
Copyright © 2011-2022 走看看