排序字符串数组会导致seg错误

时间:2013-12-11 15:55:01

标签: c++ arrays sorting

我正在尝试编写一个排序来按升序对字符串数组进行排序我的函数为:

void mySort(string list[], int size) { 
    for (int i=0; i<size; i++){
        for (int j=0; j < size-i; j++){
            if (strcmp(list[j].c_str(),list[j+1].c_str())< 0);{
                std::swap(list[j], list[j + 1]);
            }
        }
    }
}

然后使用sort的函数:

void q0run(question q){

std::string input = q.programInput;                             //Place the data file in a variable
//cout << input << endl;                                        //Test to make sure data file stored correctly - works
std::ifstream inputFile (input.c_str());                        //Open File
    if(inputFile.good()){                                       //Make sure file is open before trying to work with it
                                                                //Begin Working with information
        cout << "In File:  \t" << input << endl;
        int number_of_lines = 0;
        std::string line;
        while (std::getline(inputFile, line)){
            ++number_of_lines;
        }
        std::cout << "Number of lines in text file: " << number_of_lines << endl;
        std::string dataStorage[number_of_lines];
        inputFile.clear();
        inputFile.seekg(0);
        for(int loop=0;loop<number_of_lines;loop++){
            getline(inputFile,dataStorage[loop]);
        }
        mySort(dataStorage,number_of_lines);
        for(int loop=0;loop<number_of_lines;loop++){
            cout << dataStorage[loop] << endl;
        }
        inputFile.close();
    }else{
        cout << "Could not open file!!!" << endl;
    }

 }

当我运行该程序时,虽然它在某种程度上是段错误。不确定我做错了什么:

      In File:        data01a.txt
      Number of lines in text file: 253
      Segmentation fault (core dumped)

正在填充要排序的数组,我可以在其上运行循环并打印出未分类的任何想法?一个更简单的排序方法也会很好!谢谢!

4 个答案:

答案 0 :(得分:2)

if (strcmp(list[j].c_str(),list[j+1].c_str())< 0);{

糟糕!

  • 还有一个额外的;因此交换将始终发生;
  • 你的内循环需要少一次迭代,否则j+1会跳出结尾;
  • 所有那些C字符串转换真的是必要的吗? std::string::compare可以胜任......

答案 1 :(得分:0)

您的代码中的实际错误已在评论中得到解答。

您的“j + 1”索引可能超出界限,并且在第一次迭代中它是。因此,您需要将j循环到size-i-1或从1开始迭代i,因此..

for (int i=1; i<size; i++)

我认为这是一种练习,因为正确的排序方法只是使用std::sort

您还可以使用其他方法来比较两个字符串,其中最简单的就是它的重载运算符&lt;,因此

if( list[j] < list[j+1] )

答案 2 :(得分:0)

你有一个可能是原因的越界访问

for (int i=0; i<size; i++){
        for (int j=0; j < size-i; j++){
            if (strcmp(list[j].c_str(),list[j+1].c_str())< 0){
                std::swap(list[j], list[j + 1]);
            }

在第一次迭代中

i=0; j=size-1 =>strcmp(list[size-1].c_str(),list[size])

更改第一个循环中的停止条件:

 for (int i=0; i<size-1; i++){
            for (int j=0; j < size-i; j++){
                if (strcmp(list[j].c_str(),list[j+1].c_str())< 0){
                    std::swap(list[j], list[j + 1]);
                }

希望这会有所帮助。

答案 3 :(得分:0)

此代码在C ++ 11中执行您想要的操作:

#include <algorithm>
#include <iostream>
#include <array>
#include <string>

using namespace std;
int main()
{
    string s1 = "hello";
    string s2 = "world!";
    string s3 = "peanut";
    string s4 = "butter";

    array<string,4> ar = {s1,s2,s3,s4};
    sort(ar.begin(),ar.end());

    for(auto elem : ar)
        cout << elem << endl;
    return 0;
}