简体   繁体   中英

How do I sort two parallel arrays?

I have an array of numbers ( int numoftoys[] ) and a parallel array of strings ( string names[] ) whose entries correspond to the numbers. My assignment requires me to sort the number array so when I print, it prints the highest number first, then the next highest, and so on. How would I do that and keep the array that holds the names synced up with the number array?

Since this is homework, I'm not going to post any code, but here's an idea:

Put your data into a std::map , and iterate over that. Iteration over a map is in order of the keys.

您需要进行冒泡排序( http://en.wikipedia.org/wiki/Bubble_sort ),并在交换要排序的数组时,交换与之并行的数组。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM