1

I am new to C++. I use a Python dictionary to store the data previously, now I am working on C++. Does C++ also have a data structure like Python's dictionary?
My scenario is as follows,

We have 4 flows in the network and assign a route to each flow. Thus, in python we can:

  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)}

based on the given route (dictFlowRoute), we can know which streams are transmitted by each pair of nodes. For example, "flow1" and "flow3" are transmitted by node pair (1,2). In python, we can generate another dictionary to store these data,

dictNodePairwithFlow = { (1,2):("flow1","flow3"),
                         (2,3): ("flow1","flow4"),
                         (3,4): ("flow1","flow2"),
                         (1,5): ("flow2", "flow4"),
                         (5,3): ("flow2","flow3")}

Hence, in C++, how to present dictFlowRoute, and how to generate dictNodePairwithFlow according to the given dictFlowRoute?

HeadzzZ
  • 99
  • 1
  • 8
  • 6
    "Does C++ also have a data structure like Python's dictionary?" Yes: [`std::map`](https://en.cppreference.com/w/cpp/container/map) – MikeCAT Feb 25 '21 at 14:24
  • Even if it didn't, it's not that hard to implement yourself. – EJoshuaS - Stand with Ukraine Feb 25 '21 at 14:30
  • For reference, the language-agnostic name of the "dictionary" data structure is a Hash Map. – jfaccioni Feb 25 '21 at 14:30
  • 4
    @jfaccioni General name for "dictionary" is "associative array". Hash Map is one of implementations of that. – MikeCAT Feb 25 '21 at 14:31
  • Also see [`std::unordered_map`](https://en.cppreference.com/w/cpp/container/unordered_map) which differs from std::map primarily in its implementation — it is a hash map, whereas `std::map` is usually a tree. Now that you're a C++ programmer, you get to think a lot about what you want to optimise for. – Tommy Feb 25 '21 at 14:45
  • https://stackoverflow.com/questions/27674009/c-equivalent-of-python-dictionaries has an answer or two on this topic. – WaitingForGuacamole Feb 25 '21 at 14:48
  • Does this answer your question? [C++ equivalent of Python dictionaries](https://stackoverflow.com/questions/27674009/c-equivalent-of-python-dictionaries) – Tommy Feb 25 '21 at 14:53

1 Answers1

6

Python's Dictionary data type is an 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 implementation. And because of this, std::unordered_map is generally faster than std::map.

For your case, ill go with std::unordered_map to demonstrate. Unlike Python, we don't use Key:Value to initialize the map, instead we can use the [] operator.

#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");

Additional: std::pair, std::string

D-RAJ
  • 3,263
  • 2
  • 6
  • 24