如何对两个并行数组排序? [英] How do I sort two parallel arrays?

查看:276
本文介绍了如何对两个并行数组排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字数组(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.

这篇关于如何对两个并行数组排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆