簡體   English   中英

C ++選擇排序字符串數組

[英]C++ Selection Sorting String Arrays

我試圖了解選擇排序如何與字符串一起使用。

這是我到目前為止的內容:

#include <iostream>
#include <string>
using namespace std;


// Prototypes
void selectionSort(string arr[], int size);
void showArray(string arr[], int size);

int main() {
const int NUM_NAMES = 20;
string names[NUM_NAMES] = {"Collins, Bill", "Smith, Bart", "Allen, Jim",
                            "Griffin, Jim", "Stamey, Marty", "Rose, Geri",
                            "Taylor, Terri", "Johnson, Jill", "Allison, Jeff",
                            "Looney, Joe", "Wolfe, Bill", "James, Jean",
                            "Weaver, Jim", "Pore, Bob", "Rutherford, Greg",
                            "Javens, Renee", "Harrison, Rose", "Setzer, Cathy",
                            "Pike, Gordon", "Holland, Beth"};

// Insert your code to complete this program
cout << "The names on the list in no particlular order are: ";
showArray(names, NUM_NAMES);


// Calling the sorted array
selectionSort(names, NUM_NAMES);

// Displaying sorted array
cout << "The names in sorted order are: ";
showArray(names, NUM_NAMES);

return 0;
}

// Function to sort the string
void selectionSort(string arr[], int size) {
int startScan, minIndex, minValue;

for (startScan = 0; startScan < (size - 1); startScan++) {
    minIndex = startScan;
    minValue = arr[startScan];

    for (int index = (startScan + 1); index < size; index++) {
        if (arr[index] < minValue) {
            minValue = arr[index];
            minIndex = index;
        }
    }
    arr[minIndex] = arr[startScan];
    arr[startScan] = minValue;
    }

}

// Function to display the array's conents
void showArray(const int arr[], int size) {
    for (int count = 0; count < size; count++) {
        cout << arr[count];
        cout << "\t\n";
    }
}

如果嘗試構建它,它將失敗,並且會顯示錯誤消息。 “沒有匹配功能可調用'selectionSort'。”

但是,如果我嘗試使用int而不是字符串,則可以使它正確編譯和排序。

#include <iostream>
#include <string>
using namespace std;


// Prototypes
void selectionSort(int [], int);
void showArray(const int [], int);

int main() {
const int NUM_NAMES = 8;
int names[NUM_NAMES] = {1,3,5,7,3,5,7,9};

// Insert your code to complete this program
cout << "The names on the list in no particular order are: ";
showArray(names, NUM_NAMES);


// Calling the sorted array
selectionSort(names, NUM_NAMES);

// Displaying sorted array
cout << "The names in sorted order are: ";
showArray(names, NUM_NAMES);

return 0;
}

// Function to sort the string
void selectionSort(int 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;
    }

}

// Function to display the array's contents
void showArray(const int array[], int size) {
    for (int count = 0; count < size; count++) {
        cout << array[count];
        cout << "\t\n";
    }
}

我不太確定字符串排序在哪里出問題。 帶有問題的c ++程序的圖像

編譯錯誤很小。 void selectionSort(int [], int); 接受整數數組,它將對該數組進行排序。 但是您想對字符串數組進行排序。 您已聲明:

string names[NUM_NAMES];

您必須更改排序功能以接受字符串數組。 由於std::string支持=<>==運算符,因此修改非常容易。

void selectionSort(string arr[], int size) 
{
    int startScan, minIndex;
    string minValue;

    for(startScan = 0; startScan < (size - 1); startScan++) 
    {
        minIndex = startScan;
        minValue = arr[startScan];

        for(int index = (startScan + 1); index < size; index++) 
        {
            if(arr[index] < minValue)
            {
                minValue = arr[index];
                minIndex = index;
            }
        }
        arr[minIndex] = arr[startScan];
        arr[startScan] = minValue;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM