[英]C++ Selection Sort of an Array of String Objects
我正在嘗試為用戶輸入的字符串對象數組實現選擇排序功能。 就爭論而言,我是在正確的道路上嗎? 謝謝
void selectionSort(char ARRAY[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = ARRAY[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (ARRAY[index] < minValue)
{
minValue = ARRAY[index];
minIndex = index;
}
}
ARRAY[minIndex] = ARRAY[startScan];
ARRAY[startScan] = minValue;
}
}
您可能希望使用STL libabry並將參數聲明為
std::vector< std::string >
然后sort函數將直接工作,就像這樣
std::vector< std::string > array;
std::sort (array.begin(), array.end());
如果要對字符串對象進行排序,那么會出現很多問題。 您編寫的代碼對字符進行排序。 這個:
char ARRAY[]
是一個字符數組。 這些:
char *ARRAY[]
std::string ARRAY[]
是字符串數組。 您必須使用其中任何一個適當地更改您的功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.