#include <iostream> #include <map> using namespace std; int main(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); std::setvbuf(stdout, nullptr, _IOFBF, BUFSIZ); int num; map<int,int> numMap; cin>>num; int k = 0,v = 0; while (num--) { cin>>k>>v; numMap[k] +=v; } for(const auto &p:numMap){ cout<<p.first<<" "<<p.second<<"\n"; } return 0; }
map默认排序,下表访问会先查找,没有的话调用C++ 11之前make_pair。之后 std::forward_as_tuple(key), std::tuple<>() 原位构造