我有一个数字数组(int numoftoys[])和一个并行的字符串数组(string names[]),它们的条目与数字相对应.我的作业要求我对数字数组进行排序,以便在打印时先打印最高的数字,然后再打印第二高的数字,依此类推.我该怎么做,并使保留名称的数组与数字数组保持同步?
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:
将数据放入std::map,然后对其进行迭代.在地图上进行迭代是按键顺序进行的.
Put your data into a std::map, and iterate over that. Iteration over a map is in order of the keys.