访问地图矢量的地图元素(C++)
Accessing element of map of vector of maps (c++)
我一直致力于简单的数据库系统管理,我想出了:
std::map< std::string, std::vector < std::map < std::string,
boost::variant <std::string, size_t, double bool> > > tables;
我有一张地图(tables)的地图(记录)矢量(table),我已经写了一个函数来读取文件,但我没有非常确定如何访问单个属性。
我可以打印整个东西:
for(auto table: tables)
for(auto record : table.second)
for(auto attribute : record) {
std::cout << j.second;
我试过做类似的事情:
std::cout << tables["credentials"][2]["username"];
但是这不起作用;它只打印一个空行。
很可能是您使用了错误的地图密钥来访问数据库。
更新您的代码以打印数据库的内容,这样您也可以在地图中看到钥匙。
for(auto table: tables)
{
std::cout << "Key: " << table.first << std::endl;
for(auto record : table.second)
{
for(auto attribute : record)
{
std::cout << "Key: " << attribute.first
<< ", Value: " << attribute.second << std::endl;
}
}
}
我一直致力于简单的数据库系统管理,我想出了:
std::map< std::string, std::vector < std::map < std::string,
boost::variant <std::string, size_t, double bool> > > tables;
我有一张地图(tables)的地图(记录)矢量(table),我已经写了一个函数来读取文件,但我没有非常确定如何访问单个属性。
我可以打印整个东西:
for(auto table: tables)
for(auto record : table.second)
for(auto attribute : record) {
std::cout << j.second;
我试过做类似的事情:
std::cout << tables["credentials"][2]["username"];
但是这不起作用;它只打印一个空行。
很可能是您使用了错误的地图密钥来访问数据库。
更新您的代码以打印数据库的内容,这样您也可以在地图中看到钥匙。
for(auto table: tables)
{
std::cout << "Key: " << table.first << std::endl;
for(auto record : table.second)
{
for(auto attribute : record)
{
std::cout << "Key: " << attribute.first
<< ", Value: " << attribute.second << std::endl;
}
}
}