For instance, given the following code making two lists, "1, 2, 3, 4, 5" and "20, 9, 24, 25, 16":
list<int> index;
list<int> values;
for(int i=1; i<=5; i++) index.push_back(i);
int arr[5] = {20, 9, 24, 25, 16};
for(int i=0; i<5; i++) values.push_back(arr[i]);
for(list<int>::iterator iter=index.begin(), iter2=values.begin(); iter!=index.end(); iter++, iter2++) cout << "Index: " << *iter << " Values: " << *iter2 << endl;
Which outputs:
Index: 1 Values: 20 Index: 2 Values: 9 Index: 3 Values: 24 Index: 4 Values: 25 Index: 5 Values: 16
How can I sort the list 'values' in descending order, and concurrently sort the list 'index'? Such that Values becomes 25, 24, 20, 16, 9, and 'index' becomes 4, 3, 1, 5, 2.
I think I could do something like this using multimaps, using the values as keys and the indexes as map values. However I believe those sort every element automatically by key, and I have a large amount of elements to insert, and only need to sort a fraction of them - thus, sorting every element would be a waste of sorting time. I'm thinking in terms of partial_sort() instead.
And since I don't need random access, only sequential, I figure two lists is the way to go; but I don't know how to sort them together. If someone has any other ideas, feel free to share.
This post has been edited by Godzilla: 03 June 2008 - 05:57 PM

New Topic/Question
Reply




MultiQuote





|