简体   繁体   English

并排排序字符串数组和Int数组

[英]Sorting a string array and an Int array side by side

hope you can help. 希望能对您有所帮助。 I've got another project to do and I'm a bit stuck. 我还有另一个项目要做,我有点卡住了。 For this project we are making a simple Rock, Paper, Scissors game. 对于这个项目,我们制作一个简单的剪刀石头布游戏。 I have nearly everything set up for this, except the required highscore function. 除了所需的高分功能以外,我几乎为此设置了所有东西。 The Prof wants us to print out the top 3 highscores, in order of greatest score. 教授希望我们按照得分最高的顺序打印出前三名的高分。

How I have my code set up is that I have 2 separate arrays, highscore[] and highscoreNames[]. 我的代码设置方式是我有2个单独的数组,highscore []和highscoreNames []。 I need to figure out how to sort highscore[] from greatest to smallest, but also keep the names from highscoreNames[] paired with their score. 我需要弄清楚如何将highscore []从最大到最小排序,还要使highscoreNames []的名称与其分数配对。

Thanks for the help, and if you need anymore info ask! 感谢您的帮助,如果您需要更多信息,请咨询!

The easiest method is to use one container for the high score and the high score name: 最简单的方法是对高分和高分名称使用一个容器:

struct High_Score_Info
{
  unsigned int score;
  std::string  name;
};

//...
std::vector<High_Score_Info> scores;

This will allow you to keep the score and the name together during sorting. 这将使您在排序时将分数和名称保持在一起。

Edit 1: 编辑1:
If you are not allowed to use structures, you will need to write your own sort routine. 如果不允许使用结构,则需要编写自己的排序例程。 When you move items in the score array, also move the same items (same indices) of the names array. 当您在得分数组中移动项目时,还移动名称数组中的相同项目(相同索引)。

Structure is a better way to do this. 结构是执行此操作的更好方法。 Though just using the indexes could also help, sorting for scores and swapping in both the arrays : 尽管仅使用索引也有帮助,但在两个数组中对分数进行排序和交换:

for(i = 1; (i <= numLength); i++)
     {
          for (j=0; j < (numLength -1); j++)
         {
               if (highscore[j+1] > highscore[j])      
                { 
                    int temp = highscore[j];            
                    highscore[j] = highscore[j+1];
                    highscore[j+1] = temp;
                    string tempName = highscoreNames[j];            
                    highscoreNames[j] = highscoreNames[j+1];
                    highscoreNames[j+1] = temp;

               }
          }
     }

You can either create a formal pairing data structure, like the one @Thomas has suggested, or you can create a third array that contains the indeces of the sorted scores. 您可以创建一个正式的配对数据结构(如@Thomas建议的那样),也可以创建第三个数组,其中包含排序分数的索引。 This way you don't need to actually reorder the data arrays. 这样,您就不需要实际对数据数组进行重新排序。 For example, it might be something like this: 例如,可能是这样的:

highscore = {5, 2, 4, 1, 6};
highscoreNames = {"a", "b", "c", "d", "e"};
sortedIndeces = {4, 0, 2, 1, 3};

Then, you just simply use sortedIndeces to index. 然后,您只需使用sortedIndeces进行索引。

for (int i = 0; i < 5; i++) {
    highscore[sortedIndeces[i]]; //6, 5, 4, 2, 1
    highscoreNames[sortedIndeces[i]]; //e, a, c, b, d
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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