【发布时间】:2021-03-07 18:00:45
【问题描述】:
我应该用它来对数组进行排序。
template <class T>
class Selection
{
private:
T* ptr;
public:
Selection(T len)
{
cout << "Allocating Selection ptr\n";
//allocate memory for the pointer
ptr = new T;
*ptr = len;
};
T getobj(void)
{
return *ptr;
};
static SelectionSort(T arr[], int n)
{
//pos_min is short for position of min
T pos_min, temp;
for (int i = 0, j; i < n - 1; i++)
{
pos_min = i;//set pos_min to the current index of array
for (j = i + 1; j < n; j++)
{
if (arr[j] < arr[pos_min])
pos_min = j;
}
//pos_min keeps track of the index that min is in
//if pos_min no longer equals i than a smaller value must have been found
if (pos_min != i)
{
temp = arr[i];
arr[i] = arr[pos_min];
arr[pos_min] = temp;
}
}
};
};
在 main 中创建了数组:
int arr1[10] = { 1, 2, 7, 4, 5 };
int m = sizeof(arr1) / sizeof(arr1[0])
不知道如何将数组传递给类 Selection 中的 SelectionSort 函数(它是无效的,但我正在尝试使用静态的东西,因为互联网。到目前为止,我尝试过的所有事情都给我留下了大约 10-13 个致命错误) 我都不懂。
在类选择中将 arr1 传递给 SelectionSort 的语法是什么?
【问题讨论】: