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
相关产品推荐
相关产品推荐

