Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用选择排序对字符串进行排序_C++_Sorting - Fatal编程技术网

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

C++ 使用选择排序对字符串进行排序,c++,sorting,C++,Sorting,我正在尝试创建一个对字符串进行排序的函数,我已经创建了一个我认为应该可以工作的函数,但是toupper函数似乎没有任何效果。这里有我遗漏的东西吗 void selectionSort(string array[], int size) { int startScan, minIndex; string minValue; for(startScan=0; startScan<(size-1); startScan++) { minIndex = s

我正在尝试创建一个对字符串进行排序的函数,我已经创建了一个我认为应该可以工作的函数,但是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;
    }
}
此语句计算表达式toupperword[0]的值,然后丢弃结果

将其更改为:

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

将其更改为:

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

您没有指定函数的返回值。但不管怎么说,它不起作用,不管你怎么想,它都不起作用,这个词后来就不用了:它只是大写一个字母。 您可能想做的是将整个单词大写:

以toupper作为参数可以使用

#include <string>
#include <algorithm>
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];
      std::transform(word.begin(), word.end(), word.begin(), ::toupper);
      if (array[index] < minValue) {
        minValue = array[index];
        minIndex = index;
      }
    }
    array[minIndex] = array[startScan];
    array[startScan] = minValue;
  }
}

您没有指定函数的返回值。但不管怎么说,它不起作用,不管你怎么想,它都不起作用,这个词后来就不用了:它只是大写一个字母。 您可能想做的是将整个单词大写:

以toupper作为参数可以使用

#include <string>
#include <algorithm>
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];
      std::transform(word.begin(), word.end(), word.begin(), ::toupper);
      if (array[index] < minValue) {
        minValue = array[index];
        minIndex = index;
      }
    }
    array[minIndex] = array[startScan];
    array[startScan] = minValue;
  }
}

您没有为toupper返回值赋值。如果toupper有问题,为什么要将整个排序函数转储到不知情的读取器上?另外,您可能应该在实际代码中使用std::vector和std::sort。您没有为toupper返回值赋值。如果toupper有问题,为什么要将整个sort函数转储到不知情的读取器上?另外,您可能应该在实际代码中使用std::vector和std::sort。