[英]Gettings all keys from 2nd level of 3 levels map
#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";
}
如何獲取table1的字段名稱?
我想要這樣的東西:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.