Cattle off network - merge table record (c ++)

Title Description

Table record contains data and table index value (integer int range), make the same record table index combined value is about the same index summing operation, the output value output in accordance with the key in ascending order.

Enter a description:

To enter the number of key-value pairs 
and pairs of input value and the index value, separated by a space

Output Description:

The combined output of the key (multi-line)

#include<iostream>
#include<map>
using namespace std;
int main()
{
    map<int, int> mapA;
    int num;
    int k;
    int v;
    cin >>num;
    for(int i = 0; i < num; i++)
    {
        cin>>k>>v;
        auto ret = mapA.insert({k,v});
        if(!ret.second)
        {
            mapA[k] += v; 
        }
    }
    for(auto& v: mapA)
    {
        cout << v.first << " " << v.second << endl;
    }
    return 0;
}

 

Published 18 original articles · won praise 3 · Views 3661

Guess you like

Origin blog.csdn.net/qq_25677349/article/details/103965200