【问题标题】:C++ Selection Sorting String ArraysC++ 选择排序字符串数组
【发布时间】:2023-03-03 02:50:02
【问题描述】:

我正在尝试了解选择排序如何与字符串一起使用。

这是我目前所拥有的:

#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' 没有匹配的函数。"

但是,如果我尝试使用整数而不是字符串,我可以让它正确编译和排序。

#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";
    }
}

我不太确定字符串排序在哪里出错。 Image of c++ program with the portion that has an issue

【问题讨论】:

    标签: c++ arrays string sorting selection-sort


    【解决方案1】:

    编译错误是微不足道的。 void selectionSort(int [], int); 接受整数数组,它对该数组进行排序。但是你想对字符串数组进行排序。您已声明:

    string names[NUM_NAMES];
    

    您必须更改排序函数以接受字符串数组。由于std::string支持=、&lt;、&gt;、==运算符,因此修改相当容易。

    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;
        }
    }
    

    【讨论】:

    • 感谢您的回复。但是,我仍然遇到一些错误。我已经用错误更新了我在底部的帖子。
    • 您只更改了函数参数void selectionSort(string arr[], int size)。请注意,我还更改了 string minValue;。您还正确修复了void showArray(string arr[], int size); 的函数声明(或原型),但函数定义仍然使用void showArray(int arr[], int size){...},这自然会导致链接错误,因为编译器找不到您所说的稍后会出现。
    • 啊,非常感谢。我以为我已经改变了它,但我想没有。谢谢! :)
    猜你喜欢
    • 2011-11-19
    • 1970-01-01
    • 2011-07-27
    • 2013-05-05
    • 2014-06-13
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多