努力使用指针数组对结构数组进行排序.C++
Struggling to sort an array of structs using an array of pointers. C++
所以我试图使用指针数组对结构数组进行排序。我正在尝试根据Test
结构的int score
成员对结构进行排序。我收到一堆错误,我认为它们都与我做错的具体事情有关。(或者我只是显然对这一切是如何工作的没有丝毫概念上的了解。
以下是错误:错误列表的屏幕截图
这是代码:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
struct Test;
void selectionSort(int num, struct Test* sortArray[]);
int main()
{
const int NO_ERRRORS = 0;
int num, scoreIn;
string nameIn;
cout << "Please provide the number of test scores you would " << endl
<< "like to average and sort." << endl << endl
<< "Please limit your request to 5-20 tests: ";
cin >> num;
while ((num < 5) || (num > 20))
{
cout << "Invalid entry. Please enter an integer between 5-20: ";
cin >> num;
}
cout << endl;
Test* tests = new Test[num];
Test** sortArray = new Test*[num];
cout << "Please enter first names only with no spaces." << endl << endl;
for (int index = 0; index < num; index++)
{
cout << "Please enter the name of student " << (index + 1) << ": ";
cin >> nameIn;
cout << "Enter the test score: ";
cin >> scoreIn;
while (scoreIn < 0)
{
cout << "Invalid entry. Please enter a positive integer: ";
cin >> scoreIn;
}
cout << endl;
((*(tests + index)) = { nameIn, scoreIn }); //IntelliSense: no operator "=" matches operands. Operand types are Test = {..}
sortArray[index] = &tests[index];
}
selectionSort(num, sortArray);
for (int count = 0; count < num; count++)
cout << (sortArray[count]->score) << " ";
cout << endl;
for (int count = 0; count < num; count++)
cout << (sortArray[count]->name) << " ";
cout << endl;
delete[] tests;
cin.ignore(cin.rdbuf()->in_avail(), 'n');
cout << endl << "Press only the 'Enter' key to exit program: ";
cin.get();
return NO_ERRRORS;
}
void selectionSort(int num, struct Test* sortArray[])
{
int minIndex;
Test *minElem;
for (int scan = 0; scan < (num - 1); scan++)
{
minIndex = scan;
minElem = sortArray[scan];
for (int index = scan + 1; index < num; index++)
{
if ((*(sortArray[index])) < (*minElem)) //IntelliSense: no operator "<" matches operands. Operand types are Test < Test
{
minElem = sortArray[index];
minIndex = index;
}
}
sortArray[minIndex] = sortArray[scan];
sortArray[scan] = minElem;
}
}
struct Test
{
int num = num;
Test()
{
name = "";
score = 0;
}
string name;
int score;
};
显然,我并没有要求任何人为我做我的工作。但是有人可以指出我正确的方向吗?概念?也许指出我首先出错的地方并导致一连串的错误?
任何帮助非常感谢。
编辑:我搞砸了,忘了提到我正在工作的约束。1. 必须使用选择排序。
按对象的成员对对象的容器进行排序比这容易得多。
#include <vector>
#include <algorithm>
#include <string>
struct Test
{
Test()
: score(0)
, num(0)
{}
std::string name;
int score;
int num;
};
int main()
{
const unsigned int num = 5; // user input in your case
std::vector<Test> v(num);
//!! Assign values to items in `v`
std::sort(
v.begin(),
v.end(),
[](const Test& lhs, const Test& rhs) {
return lhs.num < rhs.num;
}
);
}
只有
1个整数的结构体这一事实并不能使它成为一个整数,它仍然是一个结构体。 为了比较对象(实际上是结构体产生的),你需要重载比较运算符,在你的例子中>运算符。
相关文章:
- 显示错误输出的简单数组排序程序
- C 使用单个函数对具有多种值类型的数组排序
- 2D数组排序,空格打乱顺序
- C 2D数组排序
- C++数组排序 - 将"bbba"和"0001"视为不正确排序的问题
- 数组排序功能
- CString 数组排序
- 简单的数组排序/检查 划分和征服版本
- C++ 中的多维数组排序
- 选择在++中对并行数组排序
- C++:二维指针数组排序:选择排序不适用于某些实例
- C++字符串数组排序
- C++中的指针数组排序算法
- 在VC++6中使用向量进行数组排序时出错,而在VC++2012中没有错误
- 将索引数组排序为主数组
- 数组排序、数组输入、数组输出
- C++数组排序,内存错误
- 在嵌入式系统上将数组排序功能从c++移植到c
- 基于其他int数组排序
- 多维数组排序c++奇怪行为