您的位置:首页 > 其它

set自定义比较函数

2016-07-31 08:47 274 查看
#include<iostream>
#include<map>
#include<string>
#include<climits>
#include<set>
#include<queue>
#include<cstring>
using namespace std;

struct node{
string s;
int cnt;
int time;
node(){
}
node(string a,int b,int c):s(a),cnt(b),time(c){
}
//	bool operator < (const node & b) const{
//		if(cnt>b.cnt) return true;
//		if(cnt<b.cnt) return false;
//		return time<b.time;
//	}
};

struct cmp{
bool operator()(const node &a,const node &b) const
{

if(a.cnt<b.cnt) return false;
if(a.cnt>b.cnt) return true;
return a.time<b.time;
}
};

int main()
{
int t,n;
cin>>t;
set<node,cmp> st;
while(t--){
cin>>n;
string s;
string ans;
int c;
st.clear();
for(int i=0;i<n;i++){
cin>>s>>c;
node nd(s,c,i);
if(st.empty()) st.insert(nd);
else{
int ok=0;
for(set<node,cmp>::iterator it=st.begin();it!=st.end();it++){
//cout<<(*it).s<<": sds"<<endl;
if((*it).s==s){
st.erase(it);
node tmp(s,(*it).cnt+c,i);
st.insert(tmp);
ok=1;
break;
}
}
if(!ok) st.insert(nd);
}
}
//		for(set<node>::iterator it=st.begin();it!=st.end();it++){
//			cout<<(*it).s<<" "<<(*it).cnt<<" "<<(*it).time<<endl;
//		}
set<node,cmp>::iterator it=st.begin();
cout<<(*it).s<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: