【问题标题】:Is there a way to link input from two different arrays?有没有办法链接来自两个不同数组的输入?
【发布时间】:2015-05-17 23:33:55
【问题描述】:

我在一些学校作业中遇到问题,我需要创建两个数组,一个用于名称,一个用于分数,允许用户输入两个数组(即输入球员姓名:;输入球员分数:) .然后我需要以降序打印数组,然后按字母升序。作为提示我们被告知:使用字符串排序函数将两个数组合并为一个然后排序。

但是我不知道如何将这两个值相互关联,这样如果我以 87 分输入 Nathan,这两个值就无法分开。

这是我到目前为止所拥有的(有些事情我试图开始工作但做不到):

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    string names[10];
    int scores[10];
    string combine[20];
    int count = 0;

    while (count < 10){
        cout << "Please enter a player's name: ";
        cin >> names[count];
        cout << "Now enter that player's score: ";
        cin >> scores[count];

        count++;

    }
    /*sort(begin(names), end(names));
    sort(begin(scores), end(scores));*/

    for (int i = 0; i < 10; i++){
        cout << names[i] << ": " << scores[i] << "\n";
    }
    system("pause");
}

【问题讨论】:

  • 这听起来很不清楚(并非不典型的作业问题)。什么是“字符串排序功能”?您最初是否被告知要使用两个单独的数组,还是可以创建一个包含 struct 对象的数组?
  • 我的老师似乎都无法给出体面的指示,因为他们想让你做什么(我觉得这很烦人)但这里是实际页面的截图:prntscr.com/76f6xh

标签: c++ arrays sorting


【解决方案1】:

您想从一开始就“链接”它们:

struct Student {
    string name;
    int score;

    bool operator<(const Student& rhs) const {
        return score > rhs.score || (score == rhs.score && name < rhs.name);
    }
};

这样,排序就很容易了:

sort(begin(students), end(students));

否则,您必须创建一个索引数组:

int indices[10];
std::iota(begin(indices), end(indices), 0);

然后排序:

std::sort(begin(indices), end(indices), [&](int a, int b){
    return scores[a] > scores[b] ||
           scores[a] == scores[b] && names[a] < names[b];
});

然后根据索引打印:

for (int idx : indices) {
   std::cout << names[idx] << " with score " << scores[idx] << '\n';
}

【讨论】:

    【解决方案2】:

    至于将两个数组合并为一个,您可以执行以下操作:

    // create a struct that of type "result"
    // that combines both name and score
    struct result
    {
        string name;
        int score;
    };
    
    int main()
    {
        string names[10];
        int scores[10];
    
        // array of your struct - same number of elements (10 not 20)
        result combine[10];
    
        int count = 0;
    
        while (count < 10){
            cout << "Please enter a player's name: ";
            cin >> names[count];
            cout << "Now enter that player's score: ";
            cin >> scores[count];
    
            count++;
    
        }
        /*sort(begin(names), end(names));
        sort(begin(scores), end(scores));*/
    
        for (int i = 0; i < 10; i++){
            cout << names[i] << ": " << scores[i] << "\n";
        }
    
        // combine into one array
        for(int i = 0; i < 10; ++i)
        {
            combine[i].name = names[i];
            combine[i].score = scores[i];
        }
    
        // Now sort the combined array
    
        system("pause");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-22
      • 2019-06-15
      • 1970-01-01
      • 2019-11-02
      • 1970-01-01
      相关资源
      最近更新 更多