#include <string>
#include <map>
#include <vector>
typedef std::map<std::string, std::map<std::string, std::map<std::string, std::string> > > SCHEMA;
int main() {
SCHEMA schema;
// Schema table
schema["table1"]["field1"]["type"] = "int";
schema["table1"]["field2"]["type"] = "bool";
schema["table2"]["field1"]["type"] = "int";
}
如何获取表 1 的字段名称?
我想要这样的东西:
fields = array(
0 => "field1",
1 => "field2"
)
最佳答案
您可以使用简单的 for
范围循环 (C++11) 遍历所有键:
std::vector<std::string> fields;
for (const auto& f : schema["table1"])
fields.emplace_back(f.first);
或者,如果您无法使用迭代器访问 C++11 功能:
std::vector<std::string> fields;
for (SCHEMA::const_iterator it = schema["table1"].cbegin(); it != schema["table1"].cend(); ++it)
fields.push_back(it->first);
关于c++ - 从 3 级 map 的第 2 级获取所有键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20365392/