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

时间:2013-12-04 06:36:02

标签: c++ arrays sorting

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 are\n";
    showArray(name, SIZE);

    // Sort the strings
    selectionSort(name, SIZE);

    // Show ordered order
    cout << "The sorted values are\n";
    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

我觉得我很亲近......感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

如果您正在学习算法,可以编写此类代码。

但请注意,在“真正的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"));

}

链接:

答案 1 :(得分:0)

selectionSort内,你需要摆脱对.at(0)

的三次调用

当您编写name[startScan].at(0)时,变量name是一个字符串数组,因此name[startScan]是一个字符串,因此name[startScan].at(0)是该字符串的第一个字母。< / p>

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

int startScan,minIndex;
string minValue;

答案 2 :(得分:0)

将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);