Sorting using pieces of a struct

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 185
Reputation: DemonGal711 is an unknown quantity at this point 
Solved Threads: 10
DemonGal711 DemonGal711 is offline Offline
Junior Poster

Sorting using pieces of a struct

 
0
  #1
Feb 25th, 2009
Ok, it's a little weird, but I'm basically trying to write a piece of code that would determine how many letters are different between two words, like cat and cot are off by one yet cat and dog are off by three.

So, I have a vector that contains all the three letter words I'm suppose to use, plus the word I'm suppose to compare them too. So, I made a new vector that using a struct that contains the string and the integer that stores the difference between the two of them words.

Is there a way I could sort that vector by the integer difference? If not, is there a way I could use the integer difference to go through the list and put the words in the right order?

I mean, there's obviously the option of going through the list multiple times until it's done. I was just curious is there was an easier way to do this.
Last edited by DemonGal711; Feb 25th, 2009 at 11:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 185
Reputation: DemonGal711 is an unknown quantity at this point 
Solved Threads: 10
DemonGal711 DemonGal711 is offline Offline
Junior Poster

Re: Sorting using pieces of a struct

 
0
  #2
Feb 25th, 2009
Actually, I think I've got it. What I did is use the sort() comparison function, passed the struct, and had it compare by the difference there. Seems to work.

  1. struct com
  2. { string data;
  3. int num;
  4. };
  5. bool com_num (com i,com j) { return (i.num<j.num); }
  6.  
  7. main()
  8. {
  9. vector <com> vec;
  10. ...
  11. sort (vec.begin(), vec.end(), com_num);
  12. ...
  13. }
Last edited by DemonGal711; Feb 25th, 2009 at 11:53 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC