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++?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With