刷题08:HJ08 用map容器

#include <iostream>
#include <map>
using namespace std;

int main()
{
    
    
	map<int, int> m;
	int n, key, v;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
    
    
		pair<int, int> data;
		cin >> data.first;
		cin >> data.second;
		if ((m.find(data.first)) != m.end())
			m[data.first] += data.second;
		else
			m[data.first] = data.second;
	}
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
	{
    
    
		cout << it->first << "   " << it->second << endl;
	}

	return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_27538633/article/details/115431390
08