C++对不同的排序函数使用相同的Index函数

C++ using same Index function for different Sort functions

本文关键字:函数 Index 排序 C++      更新时间:2023-10-16

我创建了一个小的练习程序,用于练习对从文件中读取的数据进行排序。我遇到的问题是,当我想对不同的特定列(在这种情况下是第二列)进行排序时,我会收到无法转换数据类型的错误。

我的目标是能够只找到该列的中值、最小值和最大值。现在,我知道这是一个数据类型不匹配的问题,但我想知道是否有办法绕过它?因为否则我将不得不创建另一个minIndex函数,这似乎效率很低。

这是代码:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
void readScore(ifstream & fin, double & x, int & y, string & z);
double median(double arr[], int n);
void swap(int & a, int & b);
int minIndex(double k[], int size, int startIndex);
void selectionSortAll(double k[], int l[], int size);
void selectionSortY(int l[], int size);
struct XYZ
{
    double varx[4];
    int vary[4];
    string varz[4];
};
int main()
{
    double x;
    int y;
    string z;
    ofstream fout;
    ifstream fin;
    fin.open("struct fstream.txt");
    XYZ calcX;
    XYZ calcY;
    XYZ calcZ;
    double scoreSumX = 0;
    int scoreSumY = 0;
    for (int i = 0; i < 4; i++)
    {
        readScore(fin, x, y, z);
        calcY.vary[i] = y;
        calcX.varx[i] = x;
        calcZ.varz[i] = z;
        cout << calcX.varx[i] << " ";
        cout << calcY.vary[i] << " ";
        cout << calcZ.varz[i] << endl;
        /*scoreSumX += calcX.varx[i];
        scoreSumY += calcY.vary[i];
        cout << "Current sum: " << scoreSumX << endl;
        if (i >1)
        {
            cout << "Current average: " << scoreSumX / (i + 1) << endl;
        }*/
    }
    cout << "" << endl;
    selectionSortAll(calcX.varx, calcY.vary, 4);
    for (int i = 0; i < 4; i++) {
        cout << calcX.varx[i] << " ";
        cout << calcY.vary[i] << " ";
        cout << calcZ.varz[i] << endl;
    }
    cout << median(calcX.varx, 4);
    fin.close();
    fout.close();
    return 0;
}
void readScore (ifstream & fin, double & x, int & y, string & z)
{
    fin >> x >> y >> z;
}
double median(double arr[], int n)
{
    if (n % 2 == 0)
        return arr[n / 2];
    else
        return arr[n / 2];
}
void swap(int & a, int & b) {
    int temp = a;
    a = b;
    b = temp;
}
int minIndex(double k[], int size, int startIndex) {
    int minI = startIndex;
    for (int i = startIndex; i < size; i++) {
        if (k[i] < k[minI]) {
            minI = i;
        }
    }
    return minI;
}
void selectionSortAll(double k[], int l[], int size) {
    int minI;
    for (int startIndex = 0; startIndex < size; startIndex++) {
        minI = minIndex(k, size, startIndex);
        swap(k[startIndex], k[minI]);
        swap(l[startIndex], l[minI]);
    }
}
void selectionSortY(int l[], int size) {
    int minI;
    for (int startIndex = 0; startIndex < size; startIndex++) {
        minI = minIndex(l, size, startIndex);
        swap(l[startIndex], l[minI]);
    }
}

该错误发生在最后一个函数"selectionSortY"中,因为在这种情况下,第一个参数"l"是int,但函数"minIndex"中的第一个参数是double。我是否必须创建另一个minIndex函数来索引不同的数据类型?

哦,作为参考,输入文件看起来像这样:

634 423 qwerty
115 935 jkl
968 156 asdf
439 593 iopu

感谢您抽出时间,如有任何帮助,我们将不胜感激=)

编辑:只是澄清一下,没有"选择排序"功能,程序运行得很好。"selectionSortAll"按预期对前两列进行排序。

第二版:我是编程新手,所以如果我理解起来有点慢,或者我直接不知道什么是什么,请耐心等待。此外,对于实际作业(比这个作业大得多,所以我没有在这里发布),由于这是初学者课程的第一学期,我对允许使用的内容有点限制。

尝试一个模板:

template<typename T>
int minIndex(T k[], int size, int startIndex) {
    int minI = startIndex;
    for (int i = startIndex; i < size; i++) {
        if (k[i] < k[minI]) {
            minI = i;
        }
    }
    return minI;
}

实时演示


顺便说一句,我会考虑更多地依靠<algorithm>和像vector这样的STL容器来实现您的目标。例如,查找最小元素。