首页 > 解决方案 > 将 python 字典转换为 cpp 对象

问题描述

我必须将 python 对象转换为 c++,但我不知道 python。该对象如下所示:

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

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

可以在 c++ 中使用的关闭对象是什么?

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

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

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

标签: pythonc++dictionary

解决方案


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

#include <unordered_map>
#include <vector>

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

活生生的例子

解释: astd::map或 astd::unordered_map包含元素作为对。空格不能分隔初始值设定项参数。正确的语法需要一组大括号用于该对,另一个用于向量。


推荐阅读