来自intlsy‘s省选debug方法

debugging


写代码的时候......

代码风格

唯一的要求: 自己能看懂

#define 的使用

下面会讲

工程代码 (不强求)

  • 函数专注于自己处理的数据
  • 每一个步骤分开, 别凝在一起

变量名

  • 好区分
  • 不易写错

细节问题

  • undefined behavior
    • 忘记写返回值 ( 不开 -O2 时返回奇怪的东西, 开 -O2 直接 RE )
    • main 函数也要记得返回
  • 变量名与关键字
    • 只写了一点代码: 现在改还来得及
    • 写了一大堆: 使用 namespace
    • -std=c++11?
  • 类型
    • doublelong double?
    • long longint?
    • 爆 longlong?

样例过不去?

intlsy's debugger

#define B cout << "BreakPoint" << endl;
#define O(x) cout << #x << " " << x << endl;
#define O_(x) cout << #x << " " << x << "  ";
#define Msz(x) cout << "Sizeof " << #x << " " << sizeof(x)/1024/1024 << " MB" << endl;
_tp<_tyn T>void Print( T a[] , int s , int t , char sp = ' ' , char ed = '
' ){
    if( s > t ) return;
    for( int i = s ; i < t ; i++ )
        cout << a[i] << sp;
    cout << a[t] << ed;
    cout.flush();
}
_tp<_tyn T>void Print( T a , int s = 0 , int t = -1 , char sp = ' ' , char ed = '
' ){
    if( t == -1 ) t = a.size()-1;
    for( int i = s ; i <= t ; i++ )
        cout << a[i] << sp;
    cout << ed;
    cout.flush();
}

实际应用:

#include <iostream>
using namespace std;

int Popcount( int x ){
    int ans = 0;
    while(x){
        if(x&1) ++ans;
        // x >>= 1 // OMG! I FORGOT THIS!
    }
    // Where is the return?
}
int main(){
    cout << Popcount(2333) << endl;
    return 0;
}

define

初级用法:

#define B cout << "BreakPoint" << endl;

进阶:

长得像函数一样...

#define O(x) cout << #x << " " << x << endl;

#x: 输出 x 的变量名

小实验:

#define Name_Of_Var(x) cout << #x << endl;
int mrclr_akioi = 2333;
Name_Of_Var(mrclr_akioi);

其他 # 开头的语句

#ifdef INTOJ_SAIGAO
cout << "INTOJ_SAIGAO" << endl;
#endif

gdb 调试

不会

我怎么调都调不出来

  • 使用上面的 debugger
  • 部分重构
  • 纸笔模拟 (是不是算法错了)
  • 实在不行, 就赶快做下一题

奇技淫巧

  • 多上几次卫生间
    • 禁赛警告
    • 禁赛警告
    • 禁赛警告
原文地址:https://www.cnblogs.com/jiqimin/p/10656869.html