如何同时从嵌套映射中检索键字符串
How to retrieve string of keys out of nested map at same time
我有一个嵌套的 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
但是,我没有找到与这些操作数匹配的运算符 []。这是在上面一行中学生之前的[右边。
我看到了这个 list inside map,但它没有说明如何取出物品。
编辑:这就是我最后的结果,使用我们对 String、VariantMap、VariantList、Variant 的内部定义(希望它们与标准 Variant、VariantMap、VariantList 紧密映射,但我不确定它是否确实如此或不是):
//Collin
VariantMap familyInfo0;
familyInfo0[String("%type")] = Variant("nuclear");
familyInfo0[String("%parent")] = Variant("single");
familyInfo0[String("#text")] = Variant("3");
VariantMap studentInfo0;
studentInfo0[String("name")] = Variant("Collin");
studentInfo0[String("average")] = Variant("100");
studentInfo0[String("family")] = familyInfo0;
VariantMap classInfo;
VariantList students;
VariantMap studentMap0;
studentMap0[String("student")] = studentInfo0;
VariantMap studentMap1;
studentMap1[String("student")] = studentInfo1;
students.push_back(studentMap0);
students.push_back(studentMap1);
classInfo[String("class")] = students;
Variant student0Name = classInfo[String("class")].cast<VariantList>()[0].cast<VariantMap>()[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 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)
我有一个嵌套的 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
但是,我没有找到与这些操作数匹配的运算符 []。这是在上面一行中学生之前的[右边。
我看到了这个 list inside map,但它没有说明如何取出物品。
编辑:这就是我最后的结果,使用我们对 String、VariantMap、VariantList、Variant 的内部定义(希望它们与标准 Variant、VariantMap、VariantList 紧密映射,但我不确定它是否确实如此或不是):
//Collin
VariantMap familyInfo0;
familyInfo0[String("%type")] = Variant("nuclear");
familyInfo0[String("%parent")] = Variant("single");
familyInfo0[String("#text")] = Variant("3");
VariantMap studentInfo0;
studentInfo0[String("name")] = Variant("Collin");
studentInfo0[String("average")] = Variant("100");
studentInfo0[String("family")] = familyInfo0;
VariantMap classInfo;
VariantList students;
VariantMap studentMap0;
studentMap0[String("student")] = studentInfo0;
VariantMap studentMap1;
studentMap1[String("student")] = studentInfo1;
students.push_back(studentMap0);
students.push_back(studentMap1);
classInfo[String("class")] = students;
Variant student0Name = classInfo[String("class")].cast<VariantList>()[0].cast<VariantMap>()[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 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)