You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++填充多维std::map报错问题排查及解决方案

C++多维std::map填充问题及解决方法

问题代码

尝试填充嵌套std::map时出现编译错误,相关代码如下:

#include <vector>
#include <optional>
#include <iostream>
#include <memory>
#include <map>
#include <Eigen/Dense>

using Vec3 = Eigen::Matrix<float, 3, 1>;

int main() {
    std::map<std::string, std::map<std::string, std::pair<Vec3, std::vector<std::pair<Vec3, Vec3>>>>> test;
    Vec3 color = {255, 255, 0};
    std::vector<std::pair<Vec3, Vec3>> vector(2);
    std::map<std::string, std::pair<Vec3, std::vector<std::pair<Vec3, Vec3>>>>
        second;

    second["string1"] = std::make_pair(color, vector); // 测试正常运行
    // test["string2"] = std::make_pair("string1", std::make_pair(color, vector));  // 编译错误
    test.insert(std::make_pair("string2", std::make_pair("string1", std::make_pair(color, vector)))); // 编译错误
}

报错信息

<source>:19:40: error: no match for 'operator=' 

解决方案

使用以下代码可正确填充嵌套std::map:

test["string2"].insert(std::make_pair("string1", std::make_pair(color, vector)));

内容的提问来源于stack exchange,提问作者Nitron_707

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 00:45:18