如何对两个并行数组排序?

编程入门 行业动态 更新时间:2024-10-25 13:14:33
本文介绍了如何对两个并行数组排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

如何对两个并行数组排序?

本文发布于:2023-11-30 10:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649725.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!