题目是输入人数,然后输入每个人的名字跟身高,
按身高升序排列,输入人名字,为何我的acm提交会爆出SIGSEGV
我写的代码应该无误。。。
#include<iostream>#include<string>#include<algorithm>using namespace std;struct People{ string name; int tall; People() { name=""; tall=0; }};bool cmp(People x,People y){ if(x.tall<=y.tall)return true; return false;}int main(){ int N; while(cin>>N) { People* a=new People[N+5]; for(int i=0; i<N; i++) cin>>a[i].name>>a[i].tall; sort(a,a+N,cmp); for(int i=0; i<N; i++) { cout<<a[i].name; if(i<N-1)cout<<" "; } cout<<endl; delete[] a; }}
按身高升序排列,输入人名字,为何我的acm提交会爆出SIGSEGV
我写的代码应该无误。。。
#include<iostream>#include<string>#include<algorithm>using namespace std;struct People{ string name; int tall; People() { name=""; tall=0; }};bool cmp(People x,People y){ if(x.tall<=y.tall)return true; return false;}int main(){ int N; while(cin>>N) { People* a=new People[N+5]; for(int i=0; i<N; i++) cin>>a[i].name>>a[i].tall; sort(a,a+N,cmp); for(int i=0; i<N; i++) { cout<<a[i].name; if(i<N-1)cout<<" "; } cout<<endl; delete[] a; }}