instead of struct you can use *pair<int,int>* which will make the work easier for you --
Amol Sharma Third Year Student Computer Science and Engineering MNNIT Allahabad On Wed, Aug 10, 2011 at 6:47 PM, Nitin Nizhawan <nitin.nizha...@gmail.com>wrote: > bool operator()(point a, point b){ > return a.x<b.x; > } > > remove references it should work. following is working code. > #include<iostream> > #include<algorithm> > #include<vector> > > using namespace std; > > > typedef struct > { > int x; > int y; > }point; > > struct comp_x > { > bool operator()(point a, point b){ > return a.x<b.x; > } > } compx; > > struct comp_y > { > bool operator()(point a, point b){ > return a.y<b.y; > } > } compy; > > int main() > { > vector<point> vc; > int n; > cin>>n; > point a; > > for(int i=0;i<n;i++) > { > cin>>a.x; > cin>>a.y; > vc.push_back(a); > } > cout<<endl; > > sort(vc.begin(), vc.end(), compx); > cout<<"By X\n"; > for(int i=0;i<n;i++) > { > cout<<vc[i].x<<" "<<vc[i].y<<endl; > } > sort(vc.begin(), vc.end(), compy); > cout<<"By Y\n"; > for(int i=0;i<n;i++) > { > cout<<vc[i].x<<" "<<vc[i].y<<endl; > } > > return 0; > } > > > On Wed, Aug 10, 2011 at 6:31 PM, aanchal goyal > <goyal.aanch...@gmail.com>wrote: > >> sorry, comp is either comp_x or comp_y >> >> >> On Wed, Aug 10, 2011 at 6:24 PM, Nitin Nizhawan <nitin.nizha...@gmail.com >> > wrote: >> >>> what is "comp" in your code? >>> >>> >>> On Wed, Aug 10, 2011 at 6:19 PM, aanchal goyal <goyal.aanch...@gmail.com >>> > wrote: >>> >>>> I have a vector of stuct, how to sort this vector? >>>> problem is I can't overload the '<' operator in struct definition, as i >>>> want to sort by 'x' one time, and then by 'y'. I tried to write the >>>> comparator function separatley but its no working. How to do it? >>>> >>>> #include<iostream> >>>> #include<algorithm> >>>> #include<vector> >>>> >>>> using namespace std; >>>> >>>> >>>> typedef struct >>>> { >>>> int x; >>>> int y; >>>> }point; >>>> >>>> struct comp_x >>>> { >>>> bool operator()(point &a, point &b) >>>> return a.x<b.x; >>>> } >>>> >>>> struct comp_y >>>> { >>>> bool operator()(point &a, point &b) >>>> return a.y<b.y; >>>> } >>>> >>>> int main() >>>> { >>>> vector<point> vc; >>>> int n; >>>> cin>>n; >>>> point a; >>>> >>>> for(int i=0;i<n;i++) >>>> { >>>> cin>>a.x; >>>> cin>>a.y; >>>> vc.push_back(a); >>>> } >>>> cout<<endl; >>>> sort(vc.begin(), vc.end(), comp); >>>> >>>> for(int i=0;i<n;i++) >>>> { >>>> cout<<vc[i].x<<" "<<vc[i].y<<endl; >>>> } >>>> >>>> system("pause"); >>>> return 0; >>>> } >>>> >>>> >>>> -- >>>> Regards,* >>>> Aanchal Goyal*. >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Algorithm Geeks" group. >>>> To post to this group, send email to algogeeks@googlegroups.com. >>>> To unsubscribe from this group, send email to >>>> algogeeks+unsubscr...@googlegroups.com. >>>> For more options, visit this group at >>>> http://groups.google.com/group/algogeeks?hl=en. >>>> >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Algorithm Geeks" group. >>> To post to this group, send email to algogeeks@googlegroups.com. >>> To unsubscribe from this group, send email to >>> algogeeks+unsubscr...@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >> >> >> >> -- >> Regards,* >> Aanchal Goyal*. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.