简体   繁体   English

访问地图矢量的地图元素(C ++)

[英]Accessing element of map of vector of maps (c++)

I've been working on a simple database system managment and I've come up with: 我一直在进行简单的数据库系统管理,并且提出了:

std::map< std::string, std::vector < std::map < std::string,
          boost::variant <std::string, size_t, double bool> > > tables;

I have a map (tables) of vectors (table) of maps (records) and I've allready wrote a function to read a file to it but I'm not really sure how to access the single attributes. 我有一个地图(表),一个向量(表),一个地图(记录),我已经准备好编写一个函数来读取文件,但是我不确定如何访问单个属性。

I can print the whole thing with: 我可以用以下命令打印整个内容:

for(auto table: tables)
    for(auto record : table.second)
        for(auto attribute : record) {
            std::cout << j.second;

I tried doing something like: 我尝试做类似的事情:

std::cout << tables["credentials"][2]["username"];

This however does not work; 但是,这不起作用。 it only prints a blank line. 它只打印空白行。

It's most likely that you are using wrong keys of the maps to access the database. 您很可能使用了错误的地图键来访问数据库。

Update your code to print the contents of your database so you can see the keys also in the maps. 更新代码以打印数据库的内容,以便您也可以在地图中查看键。

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;
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM