[algogeeks] STL sort

2011-08-10 Thread aanchal goyal
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?

#includeiostream
#includealgorithm
#includevector

using namespace std;


typedef struct
{
 int x;
 int y;
}point;

struct comp_x
{
 bool operator()(point a, point b)
  return a.xb.x;
}

struct comp_y
{
 bool operator()(point a, point b)
  return a.yb.y;
}

int main()
{
 vectorpoint vc;
 int n;
 cinn;
 point a;

 for(int i=0;in;i++)
 {
  cina.x;
  cina.y;
  vc.push_back(a);
 }
 coutendl;
 sort(vc.begin(), vc.end(), comp);

 for(int i=0;in;i++)
 {
  coutvc[i].x vc[i].yendl;
 }

 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.



Re: [algogeeks] STL sort

2011-08-10 Thread Nitin Nizhawan
what is comp in your code?


On Wed, Aug 10, 2011 at 6:19 PM, aanchal goyal goyal.aanch...@gmail.comwrote:

 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?

 #includeiostream
 #includealgorithm
 #includevector

 using namespace std;


 typedef struct
 {
  int x;
  int y;
 }point;

 struct comp_x
 {
  bool operator()(point a, point b)
   return a.xb.x;
 }

 struct comp_y
 {
  bool operator()(point a, point b)
   return a.yb.y;
 }

 int main()
 {
  vectorpoint vc;
  int n;
  cinn;
  point a;

  for(int i=0;in;i++)
  {
   cina.x;
   cina.y;
   vc.push_back(a);
  }
  coutendl;
  sort(vc.begin(), vc.end(), comp);

  for(int i=0;in;i++)
  {
   coutvc[i].x vc[i].yendl;
  }

  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.



Re: [algogeeks] STL sort

2011-08-10 Thread aanchal goyal
sorry, comp is either comp_x or comp_y

On Wed, Aug 10, 2011 at 6:24 PM, Nitin Nizhawan nitin.nizha...@gmail.comwrote:

 what is comp in your code?


 On Wed, Aug 10, 2011 at 6:19 PM, aanchal goyal 
 goyal.aanch...@gmail.comwrote:

 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?

 #includeiostream
 #includealgorithm
 #includevector

 using namespace std;


 typedef struct
 {
  int x;
  int y;
 }point;

 struct comp_x
 {
  bool operator()(point a, point b)
   return a.xb.x;
 }

 struct comp_y
 {
  bool operator()(point a, point b)
   return a.yb.y;
 }

 int main()
 {
  vectorpoint vc;
  int n;
  cinn;
  point a;

  for(int i=0;in;i++)
  {
   cina.x;
   cina.y;
   vc.push_back(a);
  }
  coutendl;
  sort(vc.begin(), vc.end(), comp);

  for(int i=0;in;i++)
  {
   coutvc[i].x vc[i].yendl;
  }

  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.



Re: [algogeeks] STL sort

2011-08-10 Thread aanchal goyal
got it.

On Wed, Aug 10, 2011 at 6:31 PM, aanchal goyal goyal.aanch...@gmail.comwrote:

 sorry, comp is either comp_x or comp_y


 On Wed, Aug 10, 2011 at 6:24 PM, Nitin Nizhawan 
 nitin.nizha...@gmail.comwrote:

 what is comp in your code?


 On Wed, Aug 10, 2011 at 6:19 PM, aanchal goyal 
 goyal.aanch...@gmail.comwrote:

 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?

 #includeiostream
 #includealgorithm
 #includevector

 using namespace std;


 typedef struct
 {
  int x;
  int y;
 }point;

 struct comp_x
 {
  bool operator()(point a, point b)
   return a.xb.x;
 }

 struct comp_y
 {
  bool operator()(point a, point b)
   return a.yb.y;
 }

 int main()
 {
  vectorpoint vc;
  int n;
  cinn;
  point a;

  for(int i=0;in;i++)
  {
   cina.x;
   cina.y;
   vc.push_back(a);
  }
  coutendl;
  sort(vc.begin(), vc.end(), comp);

  for(int i=0;in;i++)
  {
   coutvc[i].x vc[i].yendl;
  }

  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*.




-- 
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.



Re: [algogeeks] STL sort

2011-08-10 Thread Nitin Nizhawan
bool operator()(point a, point b){
   return a.xb.x;
 }

remove references it should work. following is working code.
#includeiostream
#includealgorithm
#includevector

using namespace std;


typedef struct
{
 int x;
 int y;
}point;

struct comp_x
{
 bool operator()(point a, point b){
   return a.xb.x;
 }
} compx;

struct comp_y
{
 bool operator()(point a, point b){
  return a.yb.y;
 }
} compy;

int main()
{
 vectorpoint vc;
 int n;
 cinn;
 point a;

 for(int i=0;in;i++)
 {
  cina.x;
  cina.y;
  vc.push_back(a);
 }
 coutendl;

 sort(vc.begin(), vc.end(), compx);
 coutBy X\n;
 for(int i=0;in;i++)
 {
  coutvc[i].x vc[i].yendl;
 }
 sort(vc.begin(), vc.end(), compy);
coutBy Y\n;
for(int i=0;in;i++)
 {
  coutvc[i].x vc[i].yendl;
 }

 return 0;
}


On Wed, Aug 10, 2011 at 6:31 PM, aanchal goyal goyal.aanch...@gmail.comwrote:

 sorry, comp is either comp_x or comp_y


 On Wed, Aug 10, 2011 at 6:24 PM, Nitin Nizhawan 
 nitin.nizha...@gmail.comwrote:

 what is comp in your code?


 On Wed, Aug 10, 2011 at 6:19 PM, aanchal goyal 
 goyal.aanch...@gmail.comwrote:

 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?

 #includeiostream
 #includealgorithm
 #includevector

 using namespace std;


 typedef struct
 {
  int x;
  int y;
 }point;

 struct comp_x
 {
  bool operator()(point a, point b)
   return a.xb.x;
 }

 struct comp_y
 {
  bool operator()(point a, point b)
   return a.yb.y;
 }

 int main()
 {
  vectorpoint vc;
  int n;
  cinn;
  point a;

  for(int i=0;in;i++)
  {
   cina.x;
   cina.y;
   vc.push_back(a);
  }
  coutendl;
  sort(vc.begin(), vc.end(), comp);

  for(int i=0;in;i++)
  {
   coutvc[i].x vc[i].yendl;
  }

  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.



Re: [algogeeks] STL sort

2011-08-10 Thread Amol Sharma
instead of struct you can use *pairint,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.comwrote:

 bool operator()(point a, point b){
return a.xb.x;
  }

 remove references it should work. following is working code.
 #includeiostream
 #includealgorithm
 #includevector

 using namespace std;


 typedef struct
 {
  int x;
  int y;
 }point;

 struct comp_x
 {
  bool operator()(point a, point b){
return a.xb.x;
  }
 } compx;

 struct comp_y
 {
  bool operator()(point a, point b){
   return a.yb.y;
  }
 } compy;

 int main()
 {
  vectorpoint vc;
  int n;
  cinn;
  point a;

  for(int i=0;in;i++)
  {
   cina.x;
   cina.y;
   vc.push_back(a);
  }
  coutendl;

  sort(vc.begin(), vc.end(), compx);
  coutBy X\n;
   for(int i=0;in;i++)
  {
   coutvc[i].x vc[i].yendl;
  }
  sort(vc.begin(), vc.end(), compy);
 coutBy Y\n;
 for(int i=0;in;i++)
  {
   coutvc[i].x vc[i].yendl;
  }

  return 0;
 }


 On Wed, Aug 10, 2011 at 6:31 PM, aanchal goyal 
 goyal.aanch...@gmail.comwrote:

 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?

 #includeiostream
 #includealgorithm
 #includevector

 using namespace std;


 typedef struct
 {
  int x;
  int y;
 }point;

 struct comp_x
 {
  bool operator()(point a, point b)
   return a.xb.x;
 }

 struct comp_y
 {
  bool operator()(point a, point b)
   return a.yb.y;
 }

 int main()
 {
  vectorpoint vc;
  int n;
  cinn;
  point a;

  for(int i=0;in;i++)
  {
   cina.x;
   cina.y;
   vc.push_back(a);
  }
  coutendl;
  sort(vc.begin(), vc.end(), comp);

  for(int i=0;in;i++)
  {
   coutvc[i].x vc[i].yendl;
  }

  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.