合并表记录

来源:互联网 发布:模拟退火算法伪代码 编辑:程序博客网 时间:2024/05/21 06:43

数据表记录包含表索引和数值,请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出。


#include<iostream>
#include<map>
using namespace  std;
 
int main(){
     intn;
     map<int,int> m;
     cin>>n;
     while(n--){
        intkey,value;
        cin>>key>>value;
        if(!m[key])
            m[key]=value;
        else
           m[key]+=value;
     }
     for(map<int,int>::iterator it=m.begin();it!=m.end();++it)
         cout<<it->first<<' '<<it->second<<endl;
    return 0;
}

0 0
原创粉丝点击