C ++: map definition with recursive values

I would like to define a card that it takes a string as a key, but the value will display again with the string as the key, but the value will display a similar nature, ..., ultimately the last card in the chain will be a display from row to strings.

I am trying to implement a JSON tag data structure for a value.

To illustrate, here are the code segments that I could find:

typedef map<string, string> str2str;
typedef map<string, str2str> str2str2str;
...
typedef map<string, str2map_or_str> str2map_or_str;

What is the idiomatic approach in C ++?

+1
source share
2 answers

The simplest approach would be to use structone that has a stringrecursive one map.

struct ParseData;
using MapValue = std::map<std::string, ParseData>;

struct ParseData
{
   std::string string_value;
   MapValue map_value;
};

, ParseData, MapValue.

+1

IMO - , . , . , boost::variant :

using map_data = boost::make_recursive_variant<
                   std::string, 
                   std::map<
                     std::string, 
                     boost::recursive_variant_
                   >
                 >::type;

- , . std::string, std::map std::string map_data ( boost::recursive_variant_). . .

json_data - :

using json_data = std::map<std::string, map_data>;

. , , boost::variant. . , .

+3

Source: https://habr.com/ru/post/1692341/


All Articles