Do you watch movies of just ask people to describe them to you, scene by scene?
Do you watch movies of just ask people to describe them to you, scene by scene?
Yes, ofcourse. But that is numbers right?
Sorting numbers or sorting strings is the same if you already have an operator > (or an operator <) that works for strings. The std::strings do support that. The way that works is that all characters in the string are actually numbers. So you can see the whole string as a long number (or a sequence of numbers).
Imagine that you were told to sort cards with a sequence of numbers:
You could do that by comparing each number, and if they are equal, compare the next number.Code:1, 3, 4 1, 2, 4 2, 8, 19 2, 9, 11 1, 2, 3
So we end up with:
Do you follow that?Code:1, 2, 3 1, 2, 4 1, 3, 4 2, 8, 19 2, 9, 11
Well, strings are just the same - ok, so different lengths come into it, but if we assume A .. Z are in ascending order in the computers notation of letters (which they will be if you are using a PC), then:
a < b < c
aaa < aab < aac < aaz
a < ab < abc < b < ba
So we could use exactly the same method to compare strings as "sequnece of numbers". If the characters in the string are not equal, one must be lesser than the other. If the current pair of letters are equal, the go to the next character, until either the end of one or both strings are found (if one string is shorter than the other, the longer is greater than the short). If they are equal all the way through, they are the same content - so neither greater nor lesser.
Given that std::string already supports compare operations such as <, > and ==, you can use this functionality to sort the numbers exactly the same way you would do when sorting numbers.
Does that explain better?
--
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.
edit: corrected error. brain glitches
my tip did not imply you need to know the string ahead of time.
the decimal conversion of the letter 'a' is guaranteed to be a lower value than the decimal conversion for the letter 'b' ... and so on.
(i think guaranteed... i mean i'm talking about general operating systems not any obscure unique cases.)
you can algorithmically test for the value of every two letter letters through a set of letters in a string, and appropriately sort all of the letters until the entire string is organized.
or you can break the string up in to an array by spaces and organize them by comparing the first letter of each array element.
or .... etc.
is that any clearer? : (
edit: matsp gave a great and very clear example. : )
Last edited by simpleid; 05-28-2008 at 06:43 PM.
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.
It is rather, actually. How many z/OS (or other IBM mainframe systems for that matter, e.g. Linux of z-series) installations are there world-wide. Compare that to the number of other OS systems there are in the world, and I think you'll find that the z-series system proportion will be rather small.
--
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.
Well how many banks & insurance companies are there in the world?
Most people probably never see a mainframe, but in one way or another, they probably use one, even if it's just by logging onto an ATM machine.
I would certainly hope the developers at my bank know the difference between ASCII and EBCDIC!