C ++:使用递归性质的值定义映射 [英] C++: Define map with values of recursive nature
问题描述
我想定义一个以字符串作为键的映射,但是值将再次是以字符串作为键的映射,但是该值将是性质相似的映射,...,最终,链中的最后一个映射将是从字符串到字符串的映射.
I'd like to define a map that it takes a string as a key, but the value would be a map again with string as the key, but the value would be a map of the similar nature,..., eventually the last map in the chain would be a map of from string to string.
我正在尝试将标记的JSON数据结构实现为值.
I'm trying to implement the JSON data structure of the tag to value.
为了说明,这是我可以提出的代码段:
To illustrate, here are the code segments that I could come up:
typedef map<string, string> str2str;
typedef map<string, str2str> str2str2str;
...
typedef map<string, str2map_or_str> str2map_or_str;
C ++中的惯用方法是什么?
What's the idiomatic approach in C++?
推荐答案
一种简单的方法是使用具有string
和递归map
的struct
.
A simplistic approach would be to use a struct
that has a string
and a recursive map
.
struct ParseData;
using MapValue = std::map<std::string, ParseData>;
struct ParseData
{
std::string string_value;
MapValue map_value;
};
然后,您可以使用ParseData
对象或MapValue
对象捕获已解析的数据.
Then, you can capture the parsed data using either a ParseData
object or a MapValue
object.
这篇关于C ++:使用递归性质的值定义映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!