简体   繁体   English

用int排序字符串数组

[英]sorting string array with int

#include <iostream>
using namespace std;

void selectionSort (float arr[], int n) {
    int i, j;
    for (i = 0; i < n; ++i) {
        for (j = i+1; j < n; ++j) {
            if (arr[i] > arr[j]) {
                arr[i] = arr[i] + arr[j];
                arr[j] = arr[i] - arr[j];
                arr[i] = arr[i] - arr[j];
            }


        }

    }
}


int main() {
    int n = 3;

    string name[n];
    float arr[n];

    for (int i = 0; i < n; i++) {
        cout << "Name of runner " << i+1 << ": ";
        cin >> name[i];

        cout << "Runner " << i+1 << "'s finishing time: ";
        cin >> arr[i];
        cout << endl;
    }


    selectionSort(arr, n);

    cout << "1st place: " << name[0] << "\t" << arr[0] << endl;
    cout << "2nd place: " << name[1] << "\t" << arr[1] << endl;
    cout << "3rd place: " << name[2] << "\t" << arr[2] << endl;
}

So when I enter the name and finishing time, only the finishing time gets sorted into order from fastest to slowest. 因此,当我输入名称和完成时间时,只有完成时间从最快到最慢才被排序。 I need help getting the runner name matched with the finished time. 我需要帮助使跑步者姓名与完成时间匹配。

Instead of two place holders name[3] and arr[3] try using vector of pairs something like below: 代替两个占位符name [3]和arr [3]尝试使用成对向量,如下所示:

std::vector<std::pair<int,string>> RunnerPairVector;

Then you can use sort to sort your pairs based on finish time like below 然后,您可以使用sort来根据完成时间对配对进行排序,如下所示

std::sort(RunnerPairVector.begin(), RunnerPairVector.end());

Sort names also corresponding to their finishing times. 排序名称也对应于其整理时间。

#include <iostream>
using namespace std;

// Pass name array also to access it..
void selectionSort (string name[], float arr[], int n) {
    int i, j;
    for (i = 0; i < n; ++i) {
        for (j = i+1; j < n; ++j) {
            if (arr[i] > arr[j]) {
                arr[i] = arr[i] + arr[j];
                arr[j] = arr[i] - arr[j];
                arr[i] = arr[i] - arr[j];
                // Swap names also corresponding to their finishing times.
                string temp = name[i];
                name[i] = name[j];
                name[j] = temp;
            }


        }

    }
}


int main() {
    int n = 3;

    string name[n];
    float arr[n];

    for (int i = 0; i < n; i++) {
        cout << "Name of runner " << i+1 << ": ";
        cin >> name[i];

        cout << "Runner " << i+1 << "'s finishing time: ";
        cin >> arr[i];
        cout << endl;
    }


    selectionSort(name, arr, n);

    cout << "1st place: " << name[0] << "\t" << arr[0] << endl;
    cout << "2nd place: " << name[1] << "\t" << arr[1] << endl;
    cout << "3rd place: " << name[2] << "\t" << arr[2] << endl;
}

What you are doing wrong here is that you are sorting the time array but not making respective changes to the name array. 您在这里做错的是您正在对时间数组进行排序,但未对名称数组进行相应的更改。 so you can pass the name array also along with time array and when you swap the time, at the same time swap the corresponding names. 因此,您还可以将名称数组与时间数组一起传递,并在交换时间时同时交换相应的名称。 something like this.. 像这样

void selectionSort(float arr[], string name[], int n) {
  int i, j;
  string temp;
  for (i = 0; i < n; ++i) {
    for (j = i + 1; j < n; ++j) {
      if (arr[i] > arr[j]) {
        arr[i] = arr[i] + arr[j];
        arr[j] = arr[i] - arr[j];
        arr[i] = arr[i] - arr[j];
        //name swapping
        temp = name[i];
        name[i] = name[j];
        name[j] = temp;
      }

    }

  }
}

and one more thing is that your function not returning this changed array. 还有一件事是您的函数不会返回此更改后的数组。 so either you return this changed array or make this time and name array globally accessible. 因此,要么返回此更改后的数组,要么使此时间和名称数组可全局访问。

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

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