Any good example of how it's done?
Any good example of how it's done?
I'm sure there are some on the board, for those willing to search....
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
std::string has < and > operators, so I believe the std::sort() function should work. If you want to sort without regards to case, that's another story.
So, implement one of the following:
Bubble-sort
Merge-sort
Quick-sort
These are googleable, so there's no point in us repeating in less clear form what you can find in for example Wikipedia.
Sorting strings or numbers or anything else is equivalent if you have a compare operation that can compare two elements of the type you are sorting.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Creating your own version of sort() is pretty simple, especially now that you know std::strings can be compared with < or > or == operators.
Depends on the object. For strings, A>B is true only if A is lexically greater; that is if A would be found later in the dictionary than B. (assuming that dictionary considers uppercase letters greater then lower case)
For a truly generic interface, the best way to do it is to have a special comparator function/function object, that is passed as an argument to the sorting function. That way you sorting algorithm can be used to sort the same list of objects by different criteria.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
Bubble sort is easy. There's nothing special about sorting strings.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
Here's a tip for you;
string woot = "blabla"
woot.at(0) == "b"
woot.at(1) == "l"
woot.at(2) == "a"
woot.at(n) == ...
char letterz = char(woot.at(0)) == 'b'
basically...
int somenumber = int(letterz) == 62
... c = 63, d = 64 ...
gl and hf with algorithms ;D
Last edited by simpleid; 05-27-2008 at 03:30 PM.
Can you write a sort for sorting an array of int's? Because std::string supports all the same comparison operators (< > etc), the only thing different is the type of the variables...
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Need another example.