AOJ 0589 - Production

冗長.

#include<iostream>
#include<vector>
#include<algorithm>

struct Product{
	std::string name;
	int amount;
};

int main(){
	std::vector<Product> v;

	int n;
	std::cin >> n;
	for(int i=0;i<n;i++){
		std::string name;
		int amount;
		std::cin >> name >> amount;
		auto it = std::find_if(v.begin(), v.end(), [&name](Product p){
					return p.name == name;
			});
		if(it == v.end()){
			v.push_back({name, amount});
		}else{
			it->amount += amount;
		}
	}

	std::sort(v.begin(), v.end(), [](Product lp, Product rp){
			if(lp.name.size() != rp.name.size())return lp.name.size() < rp.name.size();
			for(int i=0;i<lp.name.size();i++){
				if(lp.name[i] != rp.name[i])return lp.name[i] < rp.name[i];
			}
		});

	for(auto p : v){
		std::cout << p.name << " " << p.amount << std::endl;
	}
}