C++对不同的排序函数使用相同的Index函数
C++ using same Index function for different Sort functions
我创建了一个小的练习程序,用于练习对从文件中读取的数据进行排序。我遇到的问题是,当我想对不同的特定列(在这种情况下是第二列)进行排序时,我会收到无法转换数据类型的错误。
我的目标是能够只找到该列的中值、最小值和最大值。现在,我知道这是一个数据类型不匹配的问题,但我想知道是否有办法绕过它?因为否则我将不得不创建另一个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容器来实现您的目标。例如,查找最小元素。
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 返回其 INDEX 的第 个参数的模板函数
- 复制构造函数之后的flann::Index的析构函数产生访问冲突
- C++对不同的排序函数使用相同的Index函数
- 对于std::generate,可以传递函数use index