In Python, one can define a dictionary like
a = {
'a': 'hhh',
'b': 123,
'jfa': {'j': 1.5, 'r': 'string'}
}
In C++11, I see that you can
std::map<std::string, int> a = {
{"a", 1},
{"hh", 4}
};
but really I'd like the values to differ in type (and particularly allow dictionaries as values). Is there an idiom or library that does allow for this? Is anything planned for the next standard?
There currently is boost::variant
(which allows a specific set of types to fit into an object) or boost::any
which allows any type to be fit into the object. As far as I know both of them are being considered to be added to the standard library, but I'm more sure about any
.
You could consider Niels Lohmann’s excellent JSON for C++ library. Single header, permissive license, makes JSON a first class type.
Link here: https://github.com/nlohmann/json
Example:
nlohmann::json myDict = {
{"a", 1},
{"hh", 4}
}
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