简体   繁体   English

C ++选择字符串对象数组的排序

[英]C++ Selection Sort of an Array of String Objects

I'm trying to implement a selection sort function for an array of user inputted string objects. 我正在尝试为用户输入的字符串对象数组实现选择排序功能。 Am I on the right path as far as arguments go. 就争论而言,我是在正确的道路上吗? Thanks 谢谢

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

You probably want to use the STL libabry and declare the argument as 您可能希望使用STL libabry并将参数声明为

std::vector< std::string >

then the sort function will work directly, like this 然后sort函数将直接工作,就像这样

std::vector< std::string > array;
std::sort (array.begin(), array.end());

If you are sorting string objects then there's a lot of problems. 如果要对字符串对象进行排序,那么会出现很多问题。 The code you wrote sorts characters . 您编写的代码对字符进行排序。 This: 这个:

char ARRAY[]

is an array of characters. 是一个字符数组。 These: 这些:

char *ARRAY[]
std::string ARRAY[]

are arrays of strings. 是字符串数组。 You will have to change your function appropriately with either of these. 您必须使用其中任何一个适当地更改您的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM