题解 | #合并表记录#
学习到的方法
- map的遍历用法
for (auto x: mymap) {
cout << x.first << " " << x.second;
}
- map的声明及使用,pair的声明及使用
方法:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
map<int, int> mymap;
for (int i=0; i<n; i++) {
pair<int, int> p;
cin >> p.first >> p.second;
mymap[p.first] += p.second;
}
for (auto x: mymap) {
cout << x.first << " " << x.second << endl;
}
return 0;
}