4

What is the equivalent way of storing Python nested Dictionaries in C++

In Python i have:

{
    'Cloth': {
        'Tissue':None,
        'Leather': {
            'Bandage': None
        }
        },

    'Smoke': {
        'White':{
            'Smallscale': None,
            'Largescale':None
        }
    }
}

This SO Article Translating python dictionary to C++ describes it for flat dictionaries.

Community
  • 1
  • 1
user1767754
  • 23,311
  • 18
  • 141
  • 164
  • Possible duplicate of [Translating python dictionary to C++](http://stackoverflow.com/questions/1842941/translating-python-dictionary-to-c) – reticentroot Oct 14 '15 at 18:06
  • 3
    Have you read http://stackoverflow.com/questions/22274705/c-nested-map ? – Roland Spatzenegger Oct 14 '15 at 18:06
  • Are you talking a specific standard library data structure, broad "implement your own" equivalent or are meaning the data structure behind Python's implementation? (Although note that CPython is implemented in C, not C++...) – Frank V Oct 14 '15 at 18:06
  • @msanti That doesn't really answer the Q IMO – David Oct 14 '15 at 18:11
  • 1
    There's no way to do this simply without the use of `boost::variant` or a similar 3rd party variant-esque class. There's a library called Json11 which has a variant-esque class for representing json which would be perfect here. – David Oct 14 '15 at 18:13
  • Is the structure dynamic, or known at compile time? In Python it is common to do stuff with (dynamic) nested dictionaries, that you would use (static) structs or classes for in C++. If it really has to be dynamic, `boost::property_tree` might be an option for you. It comes with a JSON parser. – cdonat Oct 14 '15 at 18:22
  • @FrankV i am looking for any data-structure that has similar properties in terms of accessing and inserting data. Thanks David that is a good hint. cdonat: the Type is known in before. – user1767754 Oct 15 '15 at 01:56
  • 1
    Possible duplicate of [C++: Nested map](https://stackoverflow.com/questions/22274705/c-nested-map) – o11c Oct 08 '17 at 21:46

0 Answers0