C++
一种通用的高级编程语言,作为 C 编程语言的扩展而创建,除了用于低级别内存操作的功能外,还具有面向对象、泛型和功能性等特点。
119 个问题
include <iostream>
using namespace std;
class sort{
public:
int size;
sort()
{
cout<<"Enter the size of array for sorting";
cin>>size;
}
};
template <class T>
class Selection:public sort
{
public:
int *Array=new int[size];
void swap(int *a,int *b);
void printArray(int *Array[], int size);
void selectionSort(int *Array[], int size);
Selection()
{
cout<<"Enter the array for sorting";
for(int i=0;i<size;i++)
{
cin>>Array[i];
}
}
};
template <class T>
void Selection<T>::swap(int *a, int *b) // function to swap the the position of two elements
{
int temp = *a;
*a = *b;
*b = temp;
}
template <class T>
void Selection<T>::printArray(int *Array[], int size) // function to print an array
{
for (int i = 0; i < size; i++)
{
cout << Array[i] << " ";
}
cout << endl;
}
template <class T>
void Selection<T>::selectionSort(int *Array[], int size) {
for (int step = 0; step < size - 1; step++) {
int min = step;
for (int i = step + 1; i < size; i++)
{
if (Array[i] < Array[min]) // To sort in descending order, change > to < in this line.
min = i; // Select the minimum element in each loop.
}
swap(&Array[min], &Array[step]); // put minimum value at the correct position
printArray(Array,size) ;
}
}
int main() // driver code
{
Selection<int>l;
l.selectionSort(l.Array,l.size);
cout << "Sorted array in Acsending Order:\n";
l.printArray(l.Array,l.size);
}
Note:此问题总结整理于:In main function of selection sorting of array it is showing error that no matching function found
对于与积分相关的知识,您需要再次复习。要排序的数组是整数一维数组,因此函数参数也应该是相同类型的数组。我修改了你提供的代码,现在代码可以实现排序功能了,你可以参考一下。
顺便说一句,您可以在上传代码时使用此功能,这将帮助大家更轻松地理解您的代码。
#include <iostream>
using namespace std;
class sort {
public:
int size;
sort()
{
cout << "Enter the size of array for sorting";
cin >> size;
}
};
template <class T>
class Selection :public sort
{
public:
int* Array = new int[size];
void swap(int* a, int* b);
void printArray(int* Array, int size);
void selectionSort(int* Array, int size);
Selection()
{
cout << "Enter the array for sorting";
for (int i = 0; i < size; i++)
{
cin >> Array[i];
}
}
};
template <class T>
void Selection<T>::swap(int* a, int* b) // function to swap the the position of two elements
{
int temp = *a;
*a = *b;
*b = temp;
}
template <class T>
void Selection<T>::printArray(int* Array, int size) // function to print an array
{
for (int i = 0; i < size; i++)
{
cout << Array[i] << " ";
}
cout << endl;
}
template <class T>
void Selection<T>::selectionSort(int* Array, int size) {
for (int step = 0; step < size - 1; step++) {
int min = step;
for (int i = step + 1; i < size; i++)
{
if (Array[i] < Array[min]) // To sort in descending order, change > to < in this line.
min = i; // Select the minimum element in each loop.
}
swap(&Array[min], &Array[step]); // put minimum value at the correct position
}
}
int main() // driver code
{
Selection<int>l;
l.selectionSort(l.Array, l.size);
cout << "Sorted array in Acsending Order:\n";
l.printArray(l.Array, l.size);
}
如果答案是正确的解决方案,请点击“接受答案”并点赞。如果您对此答案有其他疑问,请点击“评论”。
注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。