python - 将 python 字典转换为 c++ 对象

标签 python c++ dictionary

我必须将 python 对象转换为 c++,但我对 python 一无所知。该对象看起来像这样:

VDIG = {
    1024 : [1,2,3,4],
    2048 : [5,6,7,8]
}

从外观上看,我认为它可能是列表 map ?

c++中可以使用的closes对象是什么?

我试过这样做,但它没有编译:

std::map<int, std::list<int>> G_Calib_VoltageDigits = {
    1024 {1,2,3},
    2048 {4, 5, 6}
};

所以我的问题是 Python 中的数据类型是什么,在 C++ 中有类似的东西的最佳方法是什么?

最佳答案

你几乎得到了正确的语法:

#include <unordered_map>
#include <vector>

std::unordered_map<int, std::vector<int>> G_Calib_VoltageDigits = {
    {1024, {1, 2, 3}},
    {2048, {4, 5, 6}}
};

Live example

说明:std::mapstd::unordered_map 包含成对的元素。空格不能分隔初始化参数。正确的语法需要一组大括号表示对,另一个大括号表示 vector 。

关于python - 将 python 字典转换为 c++ 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54909080/

相关文章:

python - 将列表转换为 DataFrame 并在 DataFrame 列内拆分嵌套字典 - Python 3.6

python - 对 Flask 应用程序类进行单元测试

python - 如何从 python 端指定 Bokeh 图的第 n 个股票代码,其中 n 是股票代码的数量

python - 从 python 创建一个 .mat 文件

c++ - 为什么 emplace_back() 会这样?

c++ - 在 C/C++ 中创建用零填充的 1GB 文件的最快方法是什么?

python - 在 Raspberry Pi3 上将数据从 Python 存储到 MySQL,获取和使用相同数据时遇到问题

c++ - 尝试使用 std::vector<std::thread> 时出现静态断言失败错误

pandas - python : Convert entire column to dictionary

列表的 Python dict 到单项匹配索引的 dict