个人觉得单步调试麻烦且费时间,所以我两年时间里F4+watch基本没怎么用过,但由于"查看变量的值"这个需求总是存在的,并且调试时通常需要显示很多东西,printf写起来又比较蛋疼,恰巧在c++11上知道了可变参数模板这个新的东西,于是果断拿过来写debug代码了。
本地调试时用的,写在头文件里,然后只在本地包含它,OJ上忽略。[local.h] =>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#ifndef _local #define _local namespace Debug { void print() { cout << endl; } template<typename T> void print(const T t) { cout << t << endl; } template<typename F, typename...R> void print(const F f, const R...r) { cout << f << " "; print(r...); } template<typename T> void print(T*p, T*q) { int d = p < q ? 1 : -1; while(p != q) { cout << *p << ", "; p += d; } cout << endl; } template<typename T1, typename T2> void print(pair<T1, T2> x) { print("first = \"", x.first, "\" second = \"", x.second, "\""); } } class Timer { private: clock_t _start; clock_t _end; public: Timer() { start(); } void start() { _start = clock(); } double get() { _end = clock(); double usedtime = double(_end - _start) / CLK_TCK; cout << "used time: " << usedtime << " s\n"; return usedtime; } }; #endif |
头文件等=>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <bits/stdc++.h> using namespace std; #ifndef ONLINE_JUDGE #include "local.h" #endif #define X first #define Y second #define pb(x) push_back(x) #define mp(x, y) make_pair(x, y) #define all(a) (a).begin(), (a).end() #define mset(a, x) memset(a, x, sizeof(a)) #define mcpy(a, b) memcpy(a, b, sizeof(a)) typedef long long ll; template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);} template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);} int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); #endif // ONLINE_JUDGE return 0; } |
结果:
时间: 2024-11-05 22:35:34