Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何同时从嵌套映射中检索键字符串_C++_Stdmap_Stdlist - Fatal编程技术网

C++ 如何同时从嵌套映射中检索键字符串

C++ 如何同时从嵌套映射中检索键字符串,c++,stdmap,stdlist,C++,Stdmap,Stdlist,我有一个嵌套的std::map学生信息,如下所示: { "class":[ "student":{ "name":"Collin", "average":"100", "family":{ "%type":"nuclear", "%parent":"divorced", "#text":"3" }

我有一个嵌套的
std::map
学生信息,如下所示:

{
 "class":[
         "student":{
             "name":"Collin",
             "average":"100",
             "family":{
             "%type":"nuclear",
             "%parent":"divorced",
             "#text":"3"
             } //family
          } //student
          "student":{
             "name":"Jamie",
             "average":"95",
             "family":{
             "%type":"nuclear",
             "%parent":"single",
             "#text":"2"
             } //family
          } //student
   ] //class
}//overall Map

where(above) - {} represents a Map
             - [] represents a List
std::string student0Name = l_mapClassOfStudents[std::string("class")][std::string("student")][0][std::string("name")]; //see error message about operator for [ before student
我试图检索如下信息:

{
 "class":[
         "student":{
             "name":"Collin",
             "average":"100",
             "family":{
             "%type":"nuclear",
             "%parent":"divorced",
             "#text":"3"
             } //family
          } //student
          "student":{
             "name":"Jamie",
             "average":"95",
             "family":{
             "%type":"nuclear",
             "%parent":"single",
             "#text":"2"
             } //family
          } //student
   ] //class
}//overall Map

where(above) - {} represents a Map
             - [] represents a List
std::string student0Name = l_mapClassOfStudents[std::string("class")][std::string("student")][0][std::string("name")]; //see error message about operator for [ before student
但是,我没有得到与这些操作数匹配的运算符[]。这是在上面一行学生前面的[右]

我看到了这个,但它没有说明如何取出物品

编辑:这就是我的结论,使用我们对字符串、VariantMap、VariantList、Variant的内部定义(希望它们与标准Variant、VariantMap、VariantList紧密对应,但我不确定是否如此):

//科林
VariantMap familyInfo0;
FamilyInfo[String(“%type”)]=变体(“nuclear”);
FamilyInfo[String(“%parent”)]=变量(“single”);
familyInfo0[String(#text”)]=变量(“3”);
变量映射学生信息0;
studentInfo[String(“name”)]=变量(“Collin”);
StudentInfo[String(“average”)]=变量(“100”);
StudentInfo[String(“family”)]=FamilyInfo;
VariantMap classInfo;
差异学生;
VariantMap studentMap0;
studentMap0[String(“student”)]=StudentInfo;
VariantMap studentMap1;
studentMap1[String(“student”)]=StudentInfo;
学生。推回(学生地图0);
学生。推回(学生地图1);
classInfo[字符串(“类”)]=学生;
Variant student0Name=classInfo[String(“class”)].cast()[0].cast()[String(“name”)];

如果学生在列表中,请使用front()检索第一个元素:

std::string student0Name = l_mapClassOfStudents[std::string("class")].front()[std::string("name")];
我认为更好的结构应该是

typedef map<string,string> Student;
typedef list<Student> Class;
typedef list<Class> School;
typedef地图学生;
typedef列表类;
类型定义列表学校;
列表通常用于遍历。 或者,您可以使用vector而不是list按编号访问它们:

typedef map<string,string> Student;
typedef vector<Student> Class;
typedef vector<Class> School;
cout << thisSchool[3][2]["name"]; // name of the 3rd student in the 4th class, if thisSchool is of type School (and enough classes and students exist)
typedef地图学生;
typedef向量类;
向量学校;

无法显示一些代码。你说l_mapClassOfStudents是一个map,但你试图像访问map一样访问它。我猜如果
class
是一个学生数组,你不需要
[std::string('student')]
你能告诉我们你容器的实际类型吗?顺便说一句,如果你还不知道的话(并且正在使用合适的编译器),您可能会发现
“s
处理字符串文字的方式使此类代码更具可读性。这很公平。但现在是2017年;也许负责编译器选择的人可以提醒一下这一点!不过,我想得到第I个元素。它不应该工作吗[0]?另外,我想将列表和地图信息串在一起。学生在列表中,但他们的子内容在地图中。列表没有随机访问运算符(改为使用向量)。否则,您可能需要使用迭代器,但我必须查看表达式的类型。此外,如果您的映射只有一个键(例如,student或class),则我认为结构没有多大意义。student级别可能有重复项(两个student标记),这就是为什么在这一点上它是一个列表。也许向量就是我需要的…要么使用向量,要么通过迭代器遍历列表。