对字符串数组进行排序会导致seg错误
Sorting array of strings causes seg fault
我正试图编写一个排序程序,以升序对字符串数组进行排序。我的函数为:
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;
}
相关文章:
- C++ - 将元素按升序插入数组的 SEG 错误
- 为什么我在使用 istream 迭代器时会出现 seg 错误?
- 字符串上的合并排序上的 Seg 错误
- 导致 Seg 错误的 SingleLinkedList 的析构函数
- g++ 9.2.1 (Linux) 会导致 seg 错误,但 Windows 上的代码块不会
- 函数抛出seg错误,我不知道为什么
- Microsoft Visual Studio 2019交叉编译CMake库 - 有效,但存在SEG错误
- 由于相同的文件名/类名存在于不同的SO中而导致的SEG错误
- 尝试访问 3D 矢量中的元素时出现 Seg 错误
- 垫子对象数组太大会导致 Seg 错误
- 为什么push_back成矢量<矢量<int>>导致 seg 错误?
- 在向量中存储字符串存在 SEG 错误
- 更改本地类名时出现 seg 错误
- 使用 ->GetString( " ") 时出现 Seg 错误,它位于单独的类中
- 将字符数组转换为 std::string 以传递到 std::bitset seg 错误
- 字符和 int 连接何时会导致 seg 错误?
- 静态数组溢出堆栈(seg 错误 11)仅在类定义内部时,否则不会..?
- .EOF 在读取文件时导致 Seg 错误:FIXXED
- 在 qt5 中使用 QSharedPointer 时出现 Seg 错误
- 为什么我的 if 语句会出现 seg 错误?