Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a python dictionary to cpp object

I have to convert a python object to c++ but I have no idea about python. The object looks like this:

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

From the look of it I think it might be a map of lists?

What is the closes object that can be used in c++ ?

I tried to do like this but it does not compile:

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

So my question is what is that data type in Python called and what is the best way to have a similar thing in c++?

like image 304
DEKKER Avatar asked Mar 05 '23 10:03

DEKKER


1 Answers

You almost got the syntax correct:

#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

Explanation: a std::map or a std::unordered_map contains elements as pair. A space cannot separate initializer arguments. The correct syntax require a set of braces for the pair, and another for the vector.

like image 198
Guillaume Racicot Avatar answered Mar 16 '23 03:03

Guillaume Racicot