c++ - 使用选择排序对字符串进行排序

标签 c++ sorting

我正在尝试创建一个对字符串进行排序的函数,并且我已经创建了一个我认为应该可以工作的函数,但是 toupper() 函数似乎没有任何效果.我在这里缺少什么吗?

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

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

        for(int index=startScan+1; index<size; index++) {
            string word = array[index];
            toupper(word[0]);
            if(array[index] < minValue) {
                minValue = array[index];
                minIndex = index;
            }
        }
        array[minIndex] = array[startScan];
        array[startScan] = minValue;
    }
}

最佳答案

toupper(word[0]);

此语句计算表达式 toupper(word[0]) 的值,然后将结果丢弃。

将其更改为:

word[0] = toupper(word[0])

关于c++ - 使用选择排序对字符串进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35169642/

相关文章:

java - QuickSort 与 MergeSort,我做错了什么?

arrays - Lua:使用不同的大小写对字符串数组进行排序

javascript - 无法获取排序项目数组以在屏幕上显示结果?

c++ - 获取Windows版本的正确方法

c++ - 将 "pointer to data"转换为 "pointer to function"

c++ - 将用于串行通信的消息存储为 C++ 可行解决方案中的成员变量?

c++ - C/C++ 链表永远不为空

python - 使用自定义键在 python 中排序元组

c++ - 具有重复键的 STL 优先级队列 - 这可能吗?

c++ - 如何使用 C++ 按第一个、第二个、第三个...列对二维数组进行排序?