Python C++ 词典列表
Python List of Dictionaries to C++
我有一个 Python 字典列表,我想将其翻译成 C++。
我的列表大致是这样的:
myList = [{"v_A": 5, "v_B": 3}, {"v_A": 1, "v_B":2}, {"v_A":0, "v_B": 0}]
我的代码是这样设计的,当我寻找例如粒子 0 的速度,我做:
v = myList[0]["v_"+letter]
其中 letter
将是一个字符串,"A"
或 "B"
,取决于我拥有的排序函数的输出。我发现这是在 Python.
中实现我的目的最方便的方法
我正在尝试将我的代码转换为 C++(有点像新手),但我不知道如何才能保持类似 "dictionary" 的功能(即通过键访问值可以构建连接字符串)和列表的好处,如 myList
.
我读了这个问题但没有多大成功,无法理解:
经过一些研究,我想到了使用无序映射向量,但我不确定如何定义它以及如何使用它。我想到了:
vector< unordered_map <string, int >> myList;
但是,我总是遇到错误。此外,我不知道如何在字典中引入值或访问元素。我是从完全错误的角度来处理这个问题吗?
编辑:
我发现如果我分段定义和插入元素,它似乎工作:
std::vector <unordered_map <string, int> > myList;
unordered_map <string, int> tempMap;
tempMap.insert(std::make_pair("v_A", 10));
myList.push_back(tempMap);
cout << myList[0]["v_A"];
returns 正确 10
。有没有办法更有效/更简单地做到这一点?
std::vector<std::unordered_map<std::string, int>> dictList; // This is a list of dicts
std::unordered_map<std::string, int> dict; // Here we create a specific dict we want to add to our list of dicts
dict["key"] = 10; // This is how you add (key, value) pair into dict in c++
dictList.push_back(dict); // This is how you add your dict to the list of dicts
对于 std::unordered_map
,您可以使用 operator[]
,如果该元素不存在,它将被插入到地图中。
向量不成立。向量是一个动态数组,您需要明确地向其添加元素,然后才能使用 operator[]
.
访问已添加的元素
std::vector<std::unordered_map<std::string, int>> myList;
myList[0]; // Invalid since the vector currently is empty.
myList.push_back({}); // Insert a default-constructed element in the vector
myList[0]; // Refers to the first element in the vector, which is a std::unordered_map<std::string, int>
myList[0]["string"] = 1; // This will create the elements if it does not already exist at mentioned above so this works fine
如果您知道您希望矢量的大小,您可以使用 std::vector::resize
或调用构造函数来设置大小。
std::vector<std::unordered_map<std::string, int>> myList(10);
// Create a vector with 10 elements, so [0-9] would be in-bounds.
std::vector<std::unordered_map<std::string, int>> myList;
myList.resize(10); // This will do the same thing but in two steps.
在列表中插入可以更简单。
在初始化时你可以做
std::vector<std::unordered_map<std::string, int>> myList {
{{"v_A", 5}, {"v_B", 3}},
{{"v_A", 1}, {"v_B", 2}},
{{"v_A", 0}, {"v_B", 0}}
};
您稍后可以使用 push_back
:
增加向量的大小
myList.push_back({{"v_A", 0}, {"v_B", 0}});
或者您可以使用与 python:
相同的方式将现有元素之一添加到字典中
myList[0]["v_C"] = 12;
但是您不能通过这种方式向矢量添加元素。 (越界访问向量会导致未定义的行为。)
元素可以像 Python 和 myList[0]["v_C"]
一样读取。
您不能像 v = myList[0]["v_"+letter]
那样使用 +
将字符或 C 样式字符串直接连接到字符串文字(取决于 letter
的类型,它可能会编译但可能不会做你期望它做的事)。但是如果你使用
using namespace std::string_literals;
在你的.cpp
文件中包含之后,你就可以写
v = myList[0]["v_"s+letter];
s
后缀然后将字符串文字转换为 std::string
,它提供了一个 +
运算符,它实际上进行了连接。
我有一个 Python 字典列表,我想将其翻译成 C++。
我的列表大致是这样的:
myList = [{"v_A": 5, "v_B": 3}, {"v_A": 1, "v_B":2}, {"v_A":0, "v_B": 0}]
我的代码是这样设计的,当我寻找例如粒子 0 的速度,我做:
v = myList[0]["v_"+letter]
其中 letter
将是一个字符串,"A"
或 "B"
,取决于我拥有的排序函数的输出。我发现这是在 Python.
我正在尝试将我的代码转换为 C++(有点像新手),但我不知道如何才能保持类似 "dictionary" 的功能(即通过键访问值可以构建连接字符串)和列表的好处,如 myList
.
我读了这个问题但没有多大成功,无法理解:
经过一些研究,我想到了使用无序映射向量,但我不确定如何定义它以及如何使用它。我想到了:
vector< unordered_map <string, int >> myList;
但是,我总是遇到错误。此外,我不知道如何在字典中引入值或访问元素。我是从完全错误的角度来处理这个问题吗?
编辑: 我发现如果我分段定义和插入元素,它似乎工作:
std::vector <unordered_map <string, int> > myList;
unordered_map <string, int> tempMap;
tempMap.insert(std::make_pair("v_A", 10));
myList.push_back(tempMap);
cout << myList[0]["v_A"];
returns 正确 10
。有没有办法更有效/更简单地做到这一点?
std::vector<std::unordered_map<std::string, int>> dictList; // This is a list of dicts
std::unordered_map<std::string, int> dict; // Here we create a specific dict we want to add to our list of dicts
dict["key"] = 10; // This is how you add (key, value) pair into dict in c++
dictList.push_back(dict); // This is how you add your dict to the list of dicts
对于 std::unordered_map
,您可以使用 operator[]
,如果该元素不存在,它将被插入到地图中。
向量不成立。向量是一个动态数组,您需要明确地向其添加元素,然后才能使用 operator[]
.
std::vector<std::unordered_map<std::string, int>> myList;
myList[0]; // Invalid since the vector currently is empty.
myList.push_back({}); // Insert a default-constructed element in the vector
myList[0]; // Refers to the first element in the vector, which is a std::unordered_map<std::string, int>
myList[0]["string"] = 1; // This will create the elements if it does not already exist at mentioned above so this works fine
如果您知道您希望矢量的大小,您可以使用 std::vector::resize
或调用构造函数来设置大小。
std::vector<std::unordered_map<std::string, int>> myList(10);
// Create a vector with 10 elements, so [0-9] would be in-bounds.
std::vector<std::unordered_map<std::string, int>> myList;
myList.resize(10); // This will do the same thing but in two steps.
在列表中插入可以更简单。
在初始化时你可以做
std::vector<std::unordered_map<std::string, int>> myList {
{{"v_A", 5}, {"v_B", 3}},
{{"v_A", 1}, {"v_B", 2}},
{{"v_A", 0}, {"v_B", 0}}
};
您稍后可以使用 push_back
:
myList.push_back({{"v_A", 0}, {"v_B", 0}});
或者您可以使用与 python:
相同的方式将现有元素之一添加到字典中myList[0]["v_C"] = 12;
但是您不能通过这种方式向矢量添加元素。 (越界访问向量会导致未定义的行为。)
元素可以像 Python 和 myList[0]["v_C"]
一样读取。
您不能像 v = myList[0]["v_"+letter]
那样使用 +
将字符或 C 样式字符串直接连接到字符串文字(取决于 letter
的类型,它可能会编译但可能不会做你期望它做的事)。但是如果你使用
using namespace std::string_literals;
在你的.cpp
文件中包含之后,你就可以写
v = myList[0]["v_"s+letter];
s
后缀然后将字符串文字转换为 std::string
,它提供了一个 +
运算符,它实际上进行了连接。