1

I was trying to sort a structure using stl sort in C++ . But i get some error . Is it that we cannot sort a structure in c++ using stl sort or is it my fault in implementation , if its my fault do let me know the correction .

Here's the code (its very small :) )

#include<iostream>
#include<algorithm>
using namespace std;
struct log {
       int sd;
       int ed;
} log[1000];
bool key(int i,int j) {
 return (log[i].ed<log[j].ed);
}
int main() {
int n,i;
cin>>n;
sort(log,log+n,key);
for (i=0;i<n;i++) cin>>log[i].sd>>log[i].ed;
for (i=0;i<n;i++) cout<<log.sd<<","<<log.ed<<endl;
system("PAUSE");
}
4

1 回答 1

0

是的你可以。但是您的比较器应该采用两个const log &(不是int)参数。

于 2012-10-31T11:55:32.543 回答