­

C++17之for循环新能力

  • 2020 年 2 月 24 日
  • 筆記

❝C++17为for循环增加遍历结构化数据的能力。❞

  • 遍历map容器
map<string, int> map;  map["Apple"] = 10;    /* key为string类型,value为int类型 */for (auto [key, value] : map) {      cout << key << " " << value << endl;  }    输出: Apple 10
  • 遍历列表pair结构化数据。
list<pair<int, double>> list;  list.push_back(make_pair(1, 9.9));    /* key为int类型,value为double类型 */for (auto [key, value] : list) {      cout << key << value << endl;  }    输出: 19.9