C++ 有像 Python 字典这样的数据结构吗?
Does C++ have a data structure like a Python dictionary?
我是 C++ 新手。我以前使用 Python 字典来存储数据,现在我正在使用 C++。 C++也有像Python的字典那样的数据结构吗?
我的场景如下,
我们在网络中有 4 个流,并为每个流分配一条路由。因此,在 python 中我们可以:
dictFlowRoute = {"flow1":(1,2,3,4), #flow1 is sent by node 1 to node 4.
"flow2":(1,5,3,4),
"flow3":(1,2,5,3),
"flow4":(2,3,1,5)}
根据给定的路由(dictFlowRoute),我们可以知道每对节点传输了哪些流。 例如“flow1”和“flow3”由节点对(1,2)传输。在python中,我们可以生成另一个字典来存储这些数据,
dictNodePairwithFlow = { (1,2):("flow1","flow3"),
(2,3): ("flow1","flow4"),
(3,4): ("flow1","flow2"),
(1,5): ("flow2", "flow4"),
(5,3): ("flow2","flow3")}
因此,在C++中,如何呈现dictFlowRoute,如何根据给定的dictFlowRoute生成dictNodePairwithFlow ?
Python 的 Dictionary
数据类型是 associative array. In C++, we have two options to choose from, std::map
and std::unordered_map
. The main difference is that std::map
uses a Self Balancing Red-Black Tree and std::unordered_map
uses a Hash Table 实现。正因为如此,std::unordered_map
通常比 std::map
.
快
对于你的情况,我用std::unordered_map
来演示。与 Python 不同,我们不使用 Key:Value
来初始化地图,而是可以使用 []
运算符。
#include <unordered_map> // For the std::unordered_map implementation.
#include <string> // For the std::string implementation.
...
std::unordered_map<std::string, std::array<int, 4>> dictFlowRoute;
dictFlowRoute["flow1"] = { 1, 2, 3, 4 };
dictFlowRoute["flow2"] = { 1, 5, 3, 4 };
dictFlowRoute["flow3"] = { 1, 2, 5, 3 };
dictFlowRoute["flow4"] = { 2, 3, 1, 5 };
std::unordered_map<std::pair<int, int>, std::pair<std::string, std::string>> dictNodePairwithFlow;
dictNodePairwithFlow[std::make_pair(1, 2)] = std::make_pair("flow1", "flow3");
dictNodePairwithFlow[std::make_pair(2, 3)] = std::make_pair("flow1", "flow4");
dictNodePairwithFlow[std::make_pair(3, 4)] = std::make_pair("flow1", "flow2");
dictNodePairwithFlow[std::make_pair(1, 5)] = std::make_pair("flow2", "flow4");
dictNodePairwithFlow[std::make_pair(5, 3)] = std::make_pair("flow2", "flow3");
补充:std::pair
, std::string
我是 C++ 新手。我以前使用 Python 字典来存储数据,现在我正在使用 C++。 C++也有像Python的字典那样的数据结构吗?
我的场景如下,
我们在网络中有 4 个流,并为每个流分配一条路由。因此,在 python 中我们可以:
dictFlowRoute = {"flow1":(1,2,3,4), #flow1 is sent by node 1 to node 4.
"flow2":(1,5,3,4),
"flow3":(1,2,5,3),
"flow4":(2,3,1,5)}
根据给定的路由(dictFlowRoute),我们可以知道每对节点传输了哪些流。 例如“flow1”和“flow3”由节点对(1,2)传输。在python中,我们可以生成另一个字典来存储这些数据,
dictNodePairwithFlow = { (1,2):("flow1","flow3"),
(2,3): ("flow1","flow4"),
(3,4): ("flow1","flow2"),
(1,5): ("flow2", "flow4"),
(5,3): ("flow2","flow3")}
因此,在C++中,如何呈现dictFlowRoute,如何根据给定的dictFlowRoute生成dictNodePairwithFlow ?
Python 的 Dictionary
数据类型是 associative array. In C++, we have two options to choose from, std::map
and std::unordered_map
. The main difference is that std::map
uses a Self Balancing Red-Black Tree and std::unordered_map
uses a Hash Table 实现。正因为如此,std::unordered_map
通常比 std::map
.
对于你的情况,我用std::unordered_map
来演示。与 Python 不同,我们不使用 Key:Value
来初始化地图,而是可以使用 []
运算符。
#include <unordered_map> // For the std::unordered_map implementation.
#include <string> // For the std::string implementation.
...
std::unordered_map<std::string, std::array<int, 4>> dictFlowRoute;
dictFlowRoute["flow1"] = { 1, 2, 3, 4 };
dictFlowRoute["flow2"] = { 1, 5, 3, 4 };
dictFlowRoute["flow3"] = { 1, 2, 5, 3 };
dictFlowRoute["flow4"] = { 2, 3, 1, 5 };
std::unordered_map<std::pair<int, int>, std::pair<std::string, std::string>> dictNodePairwithFlow;
dictNodePairwithFlow[std::make_pair(1, 2)] = std::make_pair("flow1", "flow3");
dictNodePairwithFlow[std::make_pair(2, 3)] = std::make_pair("flow1", "flow4");
dictNodePairwithFlow[std::make_pair(3, 4)] = std::make_pair("flow1", "flow2");
dictNodePairwithFlow[std::make_pair(1, 5)] = std::make_pair("flow2", "flow4");
dictNodePairwithFlow[std::make_pair(5, 3)] = std::make_pair("flow2", "flow3");
补充:std::pair
, std::string