Correct me if I'm wrong, but this is what I think you're trying to do.
You're trying to take a word (say, "matchmaker"), sort the letters ("aacehkmmrt"), and then output the new position of each letter in the original word? (e.g. the first 'm' ended up as the 7th letter, the 'a' after that ended up as the 1st letter, etc.)
If that's the case, then here's what you can do.
First, number off the letters of the original word:
m a t c h m a k e r
1 2 3 4 5 6 7 8 9 10 |
Then, sort the word, moving the numbers along with the letters:
a a c e h k m m r t
2 7 4 9 5 8 1 6 10 3 |
Then, for each
i from 1 to the size of the word, take the number
ki associated with the
ith letter of the new (sorted) word and assign the value
i to the
kith letter of the original word.
For instance, the 4
th letter of the sorted word is 'e', which has a value of 9. So take the 9
th letter of the original word (which, of course, is 'e') and assign that a value of 4.
Does that make sense?
As for keeping track of the numbers along with the letters, you have a few options:
1) Keep two
std::vectors, one for letters and one for numbers.
Implement your own sort function that takes both as input, and whenever you would swap two letters in the sorting process, also swap the corresponding numbers in the other
vector.
2) Keep one
std::vector of
std::pairs of letters and numbers.
Implement your own sort function that just looks at the letters when sorting.
3) Same as #2, except use
std::sort with a custom compare function instead of implementing your own sorting function.