首页 > 其他分享 >从一次DEBUG体会modernCPP

从一次DEBUG体会modernCPP

时间:2024-02-26 15:34:42浏览次数:16  
标签:std 体会 vector int auto modernCPP ind edge DEBUG

今天写树形dp,用匿名函数出现了极其玄学的报错,modernCPP给我上了一课

note: 'solve()::<lambda(auto:23, int, int)>::~()' is implicitly deleted because the default definition would be ill-formed

源代码

void solve() {
    int n;
    std::cin >> n;
    std::vector<int> edge[n + 1];
    std::vector<int> F(n + 1);
    std::vector<int> ind(n + 1);
    for (int u, v, i = 0; i < n - 1; i++) {
        std::cin >> u >> v;
        edge[u].emplace_back(v);
        edge[v].emplace_back(u);
        ind[u]++; ind[v]++;
    }
    auto dfs = [&](auto self, int x, int father) -> void {
        if (ind[x] == 1 && x != 1) {F[x] = 1; return ;}
        for (auto& to : edge[x]) if (to != father) {
            self(self, to, x);
            F[x] += F[to]; 
        }
        return ;
    };
    dfs(dfs, 1, 0);
    int q;
    std::cin >> q;
    while(q--) {
        int x, y;
        std::cin >> x >> y;
        std::cout << F[x] * F[y] << '\n';
    }
}

排查

由于报错是析构函数出错,比较难以排查,所以就枚举了所有情况,发现以下几种写法可以通过编译。

  • 声明 function 类型

    std::function<void(int, int)> dfs [&](int x, int father) -> void {
        if (ind[x] == 1 && x != 1) {F[x] = 1; return ;}
    	for (auto& to : edge[x]) if (to != father) {
    		dfs(to, x);
    		F[x] += F[to]; 
    	}
    	return ;
    };
    

    从声明类型后就可以通过编译我感觉是编译器在 auto 推断时出了错误,然而却并非如此。

  • 注明捕获对象

    auto dfs = [&ind, &edge, &F](auto self, int x, int father) -> void {
    	if (ind[x] == 1 && x != 1) {F[x] = 1; return ;}
    	for (auto& to : edge[x]) if (to != father) {
    		self(self, to, x);
    		F[x] += F[to]; 
    `	}
    	return ;
    };
    

    这里找出了真正的原因,自动捕获到了脏东西

解决

自动捕获到了什么脏东西?

对捕获对象逐一排查后,发现对于邻接表存图的 edge,把大小改成常量或者使用 vector 嵌套后就能通过编译。

这里涉及了 CPP 为了兼容 C 而留下的遗留问题

VLA虽然允许存在,但是modernCPP的接口没有义务再兼容VLA

改成嵌套 vector 后即可通过

void solve() {
    int n;
    std::cin >> n;
    std::vector<std::vector<int>> edge(n + 1, std::vector<int>());
    std::vector<int> F(n + 1);
    std::vector<int> ind(n + 1);
    for (int u, v, i = 0; i < n - 1; i++) {
        std::cin >> u >> v;
        edge[u].emplace_back(v);
        edge[v].emplace_back(u);
        ind[u]++; ind[v]++;
    }
    auto dfs = [&](auto self, int x, int father) -> void {
        if (ind[x] == 1 && x != 1) {F[x] = 1; return ;}
        for (auto& to : edge[x]) if (to != father) {
            self(self, to, x);
            F[x] += F[to]; 
        }
        return ;
    };
    dfs(dfs, 1, 0);
    int q;
    std::cin >> q;
    while(q--) {
        int x, y;
        std::cin >> x >> y;
        std::cout << F[x] * F[y] << '\n';
    }
}

标签:std,体会,vector,int,auto,modernCPP,ind,edge,DEBUG
From: https://www.cnblogs.com/kdlyh/p/18034430

相关文章

  • GCC优化debug
    GCC编译器提供了多种优化选项,可以帮助改善代码的性能和效率,但当优化等级设置不对时,会导致coredump问题,本文对个人理解和网络上对GCC总结比较完善的文档做个记录备份,侵权联删GCC优化出现的问题个人分析思路:1、通过addr2line、nm等方法分析出现堆栈对应的代码段,分析代码逻辑,若代码......
  • 第2单 - 实验 debug 命令
         编写命令 以汇编方式向内存中写入数据 A命令 向0010:0000 地址写入汇编  U命令查看刚录入的命令 查看内存中的汇编语言 U命令   修改CS,IP,到新录入的程序   T命令 执行  2.    3. Dff......
  • 状态机模式的初步了解及学习心得体会
    这种模式,解决的是,程序在不同状态切换及增加新的状态时,需要改很多代码的问题。它能用简单的逻辑控制程序从一个状态切换为其他被允许的状态,我昨天在网上看的一个例子,一个播放器,有播放,暂停,关闭的状态。处于关闭状态时,只能响应播放的代码;处于暂停状态时,能响应关闭和播放代码;处于播放......
  • 记录一个Mono Runtime与Mono Debugger-Agent的兼容性问题
      在mono集成到C++应用程序中时,可以通过一些参数来配置mono启动时暂停,并在调试器链接上来后再继续运行。这对于调试一些启动时机非常早的代码会非常有用。这通过给mono_jit_parse_options传递参数embedding和suspend来实现。staticvoidprint_usage(void){PRINT_ERR......
  • phpstorm开启debug断点调试模式
    查看php版本查看自己php的版本,使用:phpinfo()函数<?phpechophpinfo();XdebugXdebug:Support—TailoredInstallationInstructions右击查看index.php源代码并复制到Xdebug中点击分析查看分析结果修改php配置文件vi/opt/homebrew/etc/php/7.4/conf.d/99-xdebug.......
  • C++文件输入输出的简单实现(Debug)
    1.前言:        文件输入输出是个很有用的东西,有时比赛时要有:要求使用文件输入输出,还有时候……    遇到这种时间限制非常恶心的题目:手动测试会有误差……    文件输入输出是个很好的选择!2.写法:C    C语言的写法有点复杂,涉及文件指针,本文不......
  • Debug: tf distribute strategy parameter server: tfx component trainer: OutOfRang
    [ERROR:tfdistributestrategyparameterserver:tfxcomponenttrainer:OutOfRangeError(),Node:'cond/IteratorGetNext'Endofsequence]logofpodtfx-component-trainer:2024-02-1409:43:48.571820:W./tensorflow/core/distributed_runtime/eager/......
  • Keil5在Debug下如何实时查看变量的值
    在Debug模式下查看某个变量的值很简单,只需把需要查看的变量添加到watch,有一点要注意的是该变量必须是全局变量才能实时显示,具体操作如下图。1、把鼠标光标移到要查看的变量处;2、点击鼠标右键,在弹出的窗口中选择Add‘ADC _Value’to;3、选择显示的窗口,有两个窗口选择。变量......
  • Debug: tf distribute strategy parameter server: NOT_FOUND: No such file or dire
    [ERROR:NOT_FOUND:/tfx/tfx_pv/pipelines/detect_anomolies_on_wafer_tfdv_schema/ImportExampleGen/examples/67/Split-train/data_tfrecord-00000-of-00001.gz;Nosuchfileordirectory]logofpodtfx-trainer-component:ERROR:tensorflow:/job:worker/task:0en......
  • Debug : kfp.Client().upload_pipeline(): Failed to start a transaction to create
    [ERROR:Failedtostartatransactiontocreateanewpipelineandanewpipelineversion:dialtcp:lookupmysqlon10.96.0.10:53:nosuchhost","]>>>kfp.Client().upload_pipeline("/home/maye/pipeline_wafer_distribute.yaml",......