| | |
Sorting using pieces of a struct
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 185
Reputation:
Solved Threads: 10
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.
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.
•
•
Join Date: Apr 2008
Posts: 185
Reputation:
Solved Threads: 10
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.
C++ Syntax (Toggle Plain Text)
struct com { string data; int num; }; bool com_num (com i,com j) { return (i.num<j.num); } main() { vector <com> vec; ... sort (vec.begin(), vec.end(), com_num); ... }
Last edited by DemonGal711; Feb 25th, 2009 at 11:53 pm.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Please correct the error in my program. When i run the program on turbo c++ 3,.
- Next Thread: Some questions about C++
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





