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

Sorting array of strings causes seg fault

本文关键字:seg 错误 排序 字符串 数组      更新时间:2023-10-16

我正试图编写一个排序程序,以升序对字符串数组进行排序。我的函数为:

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]);
            }
        }
    }
}

然后是使用排序的函数:

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)

要排序的数组正在填充中,我可以在它上运行一个循环,并将其全部打印出来,有什么想法吗?更简单的排序方法也可以!谢谢

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

哎呀!

  • 有一个额外的;,所以交换总是会发生
  • 您的内部循环需要少一次迭代,否则j+1会跳到末尾
  • 所有这些C字符串转换真的有必要吗
    std::string::compare会完成任务

代码中的实际错误在注释中得到了回答。

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

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

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

还为您提供了比较两个字符串的替代方法,其中最简单的就是它的重载运算符<,因此

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

您有一个越界访问,这可能是的原因

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]);
                }

希望这能有所帮助。

这段代码在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;
}