久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何循環遍歷地圖的 C++ 地圖?

How can I loop through a C++ map of maps?(如何循環遍歷地圖的 C++ 地圖?)
本文介紹了如何循環遍歷地圖的 C++ 地圖?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

如何在 C++ 中循環遍歷 std::map?我的地圖定義為:

How can I loop through a std::map in C++? My map is defined as:

std::map< std::string, std::map<std::string, std::string> >

例如上面的容器保存的數據是這樣的:

For example, the above container holds data like this:

m["name1"]["value1"] = "data1";
m["name1"]["value2"] = "data2";
m["name2"]["value1"] = "data1";
m["name2"]["value2"] = "data2";
m["name3"]["value1"] = "data1";
m["name3"]["value2"] = "data2";

如何遍歷此地圖并訪問各種值?

How can I loop through this map and access the various values?

推薦答案

老問題,但從 C++11 開始,其余答案已過時 - 您可以使用 范圍基于 for 循環 只需執行:

Old question but the remaining answers are outdated as of C++11 - you can use a ranged based for loop and simply do:

std::map<std::string, std::map<std::string, std::string>> mymap;

for(auto const &ent1 : mymap) {
  // ent1.first is the first key
  for(auto const &ent2 : ent1.second) {
    // ent2.first is the second key
    // ent2.second is the data
  }
}

這應該比早期版本更干凈,并避免不必要的副本.

this should be much cleaner than the earlier versions, and avoids unnecessary copies.

有些人贊成用引用變量的顯式定義替換注釋(如果未使用,則會被優化掉):

Some favour replacing the comments with explicit definitions of reference variables (which get optimised away if unused):

for(auto const &ent1 : mymap) {
  auto const &outer_key = ent1.first;
  auto const &inner_map = ent1.second;
  for(auto const &ent2 : inner_map) {
    auto const &inner_key   = ent2.first;
    auto const &inner_value = ent2.second;
  }
}

這篇關于如何循環遍歷地圖的 C++ 地圖?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 亚洲在线免费观看 | 久久成人精品视频 | 成人国产精品久久 | 欧美日韩一区在线 | 国产伦精品一区二区三毛 | 久久国际精品 | 婷婷综合在线 | 亚洲精品久久久久久下一站 | 美女视频h | 久久精品99 | 在线观看www高清视频 | 国产精品一区二区三区四区 | 欧美1区| 一级黄色毛片 | 范冰冰一级做a爰片久久毛片 | 国产电影一区二区三区爱妃记 | 国产精品视频免费看 | 亚洲精品视频免费 | 亚洲精品久久久久久国产精华液 | 动漫www.被爆羞羞av44 | 午夜小视频在线播放 | 亚洲午夜视频在线观看 | 日韩午夜一区二区三区 | 91精品国产日韩91久久久久久 | 青青草这里只有精品 | 中文字幕av第一页 | 高清免费在线 | 亚洲狠狠 | 久久国产精品-久久精品 | 亚洲一区免费在线 | 国产精品1区 | 国产精品久久久久久久午夜片 | 蜜桃av鲁一鲁一鲁一鲁 | 国产一区二区精品在线 | 国产男女猛烈无遮掩视频免费网站 | 在线观看日本网站 | 久久中文字幕一区 | 中文字幕精品视频 | 日韩av成人 | 国产精品久久国产精品99 gif | 日韩中文字幕免费在线观看 |