一次调用经过了哪几层
(本条路线统一用 g++ -std=c++17 -O0 -Wall 编译)⚠️ 判题机上没法让你在 IDE 里跳转,所以这一节练的是**同一件事的另一半**:**在每层留一个记号,让执行路径自己打出来**。会读会摆记号,换到有 IDE 和调试器的地方一样用。
#include <iostream>
#include <string>
/* 读别人的代码时最想知道的:这一次调用到底经过了哪几层、每层做了什么 */
static std::string path;
static void mark(const char *n) { if (!path.empty()) path += ">"; path += n; }
static int leaf(int x) { mark("leaf"); return x + 1; }
static int mid(int x) { mark("mid"); return leaf(x) * 2; }
static int top(int x) { mark("top"); return mid(x) - 3; }
int main() {
int r = top(4);
std::cout << r << "/" << path << "\n";
return 0;
}
全部评论