- 遍历map容器
#include <map>
#include <iostream>
using namespace std;
map<string, int> map;
map["Apple"] = 10;
/* key为string类型,value为int类型 */for (auto [key, value] : map) {
cout << key << " " << value << endl;
}
输出: Apple 10
- 遍历列表pair结构化数据。
#include <iostream>
#include <list>
using namespace std;
ist<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