C++ 对字符而不是整个字符串进行排序

C++ Sorting characters rather than entire strings

本文关键字:字符串 排序 字符 C++      更新时间:2023-10-16
void selectionSort(string [], int);
void showArray(string [], int);
int main()
{
   const int SIZE = 20;
   string name[SIZE] = 
   {"Collins, Bill",  "Smith, Bart",  "Michalski, Joe", "Griffin, Jim",
    "Sanchez, Manny", "Rubin, Sarah", "Taylor, Tyrone", "Johnson, Jill", 
    "Allison, Jeff",  "Moreno, Juan", "Wolfe, Bill",    "Whitman, Jean",
    "Moretti, Bella", "Wu, Hong",     "Patel, Renee",   "Harrison, Rose",
    "Smith, Cathy",   "Conroy, Pat",  "Kelly, Sean",    "Holland, Beth"};
    // Show initial order
    cout << "The unsorted values aren";
    showArray(name, SIZE);
    // Sort the strings
    selectionSort(name, SIZE);
    // Show ordered order
    cout << "The sorted values aren";
    showArray(name, SIZE);

    return 0;
}


    void selectionSort(string name[], int size)
    {
        char startScan, minIndex, minValue;
        for (startScan = 0; startScan < (size - 1); startScan++)
        {
            minIndex = startScan;
            minValue = name[startScan].at(0);
            for(int index = startScan + 1; index < size; index++)
            {
                if (name[index].at(0) < minValue)
                {
                    minValue = name[index].at(0);
                    minIndex = index;
                }
            }
            name[minIndex] = name[startScan];
            name[startScan].at(0) = minValue;
        }
    }

    void showArray(string name[], int size)
    {
        for (int count = 0; count < size; count++)
            cout << name[count] << endl;
            cout << endl;
    }

我希望这个程序显示未排序的字符串数组,对字符串进行排序,显示排序数组。相反,它只是对名称的第一个字母进行排序。这样:

The unsorted values are Collins, Bill Smith, Bart Michalski, Joe Griffin, Jim Sanchez, Manny Rubin, Sarah Taylor, Tyrone Johnson, Jill Allison, Jeff Moreno, Juan Wolfe, Bill Whitman, Jean Moretti, Bella Wu, Hong Patel, Renee Harrison, Rose Smith, Cathy Conroy, Pat Kelly, Sean Holland, Beth

The sorted values are Aollins, Bill Cmith, Bart Cichalski, Joe Griffin, Jim Hanchez, Manny Hubin, Sarah Jaylor, Tyrone Kaylor, Tyrone Mmith, Bart Mmith, Bart Molfe, Bill Phitman, Jean Rmith, Bart Su, Hong Shitman, Jean Su, Hong Thitman, Jean Wolfe, Bill Whitman, Jean Wu, Hong

我觉得我离我很近...任何帮助,不胜感激。

如果你正在学习算法,编写这样的代码是可以的。

但是请注意,在"实际C++"(即生产代码(中,我们倾向于:

  • 使用 STL 容器而不是数组
  • 使用 STL 算法而不是原始循环

法典:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
int main(int argc, char** argv)
{
    std::vector<std::string> names = 
    { 
        "Collins, Bill", "Smith, Bart", "Michalski, Joe", "Griffin, Jim",
        "Sanchez, Manny", "Rubin, Sarah", "Taylor, Tyrone", "Johnson, Jill",
        "Allison, Jeff", "Moreno, Juan", "Wolfe, Bill", "Whitman, Jean",
        "Moretti, Bella", "Wu, Hong", "Patel, Renee", "Harrison, Rose",
        "Smith, Cathy", "Conroy, Pat", "Kelly, Sean", "Holland, Beth" 
    };
    std::sort(names.begin(), names.end(), std::less<>());
    std::copy(names.begin(), names.end(), 
              std::ostream_iterator<std::string>(std::cout, "n"));
}

链接:

  • 容器库
  • 算法库
  • 为什么我应该使用容器类而不是简单的数组?
  • Scott Meyers:STL 算法与手写循环

使用带有自定义比较功能的 std::sort

  1. 使用 std::vector 保存 Names 数组

  2. 定义自定义比较函数

    bool myCompare(string i,string j) 
    { 
            if(i.length() >= 1 &&
                j.length() >=1)
            {
                return (i[0]<j[0]); 
            }
            return false;
    }
    
  3. 使用 std::sort

    std::sort (vecName.begin(), vecName.end(), myCompare);
    

selectionSort内部,您需要摆脱对.at(0)的三个调用

当你写name[startScan].at(0)时,变量name是一个字符串数组,所以name[startScan]是一个字符串,所以name[startScan].at(0)是该字符串的第一个字母。

您需要按字符串进行比较和分配,而不是按字符。 摆脱.at(0)的将做到这一点,它会给您留下类型错误,需要一些调整,因此char startScan, minIndex, minValue;应该是

int startScan,minIndex;
string minValue;