无法在地图的地图的 stl 地图中插入数据
unable to insert data in a stl map of map of map
我有以下地图声明:
std::map<std::string, std::map<std::string, std::map<std::string, std::string>>> m;
我试过按以下方式插入数据:
m.insert({ "HARDWARE\DESCRIPTION\System\BIOS", { "REG_SZ", { "SystemSKU", "SystemSKU" } } });
但它不起作用,插入数据的正确语法是什么?
如果你不能让它与初始化列表一起工作,你可以使用经典方法:
m["HARDWARE\DESCRIPTION\System\BIOS"]["REG_SZ"]["SystemSKU"] = "SystemSKU";
希望对您有所帮助。
This 列表初始化对你有用:
std::map<std::string, std::map<std::string, std::map<std::string, std::string>>> m{{"HARDWARE\DESCRIPTION\System\BIOS",
{{ "REG_SZ", {{ "SystemSKU", "SystemSKU" }} }}
}};
或者,使用插入
m.insert({"HARDWARE\DESCRIPTION\System\BIOS",
{{ "REG_SZ", {{ "SystemSKU", "SystemSKU" }} }} //create inner maps using double braces
});
它使用 std::map 的列表构造函数(采用 std::initializer_list> 的构造函数)。
虽然读和写有点棘手。
请注意,双括号 {{ 或 }} 一起使用。 内层 {} 标记地图中的单项,外层 {} 表示 整个地图 。
为了简单起见,喜欢其他答案。
我有以下地图声明:
std::map<std::string, std::map<std::string, std::map<std::string, std::string>>> m;
我试过按以下方式插入数据:
m.insert({ "HARDWARE\DESCRIPTION\System\BIOS", { "REG_SZ", { "SystemSKU", "SystemSKU" } } });
但它不起作用,插入数据的正确语法是什么?
如果你不能让它与初始化列表一起工作,你可以使用经典方法:
m["HARDWARE\DESCRIPTION\System\BIOS"]["REG_SZ"]["SystemSKU"] = "SystemSKU";
希望对您有所帮助。
This 列表初始化对你有用:
std::map<std::string, std::map<std::string, std::map<std::string, std::string>>> m{{"HARDWARE\DESCRIPTION\System\BIOS",
{{ "REG_SZ", {{ "SystemSKU", "SystemSKU" }} }}
}};
或者,使用插入
m.insert({"HARDWARE\DESCRIPTION\System\BIOS",
{{ "REG_SZ", {{ "SystemSKU", "SystemSKU" }} }} //create inner maps using double braces
});
它使用 std::map 的列表构造函数(采用 std::initializer_list> 的构造函数)。
虽然读和写有点棘手。
请注意,双括号 {{ 或 }} 一起使用。 内层 {} 标记地图中的单项,外层 {} 表示 整个地图 。
为了简单起见,喜欢其他答案。