C++ 换行符上的合并排序错误中断

C++ Merge sorting error breaks on newline text

本文关键字:排序 错误 中断 合并 换行符 C++      更新时间:2023-10-16

我正在对字符串向量进行合并排序。到目前为止,我在函数中实现了大部分算法。

我使用<输入从命令提示符获取输入.txt将输入从文本文件传递到>output.txt 输出。

文本文件如下所示(请注意空行(:

你好 世界。

夏天的3天!

对于每一行文本,它入到一个向量中,然后将该文本向左旋转并推回向量中,并重复直到n-1。然后我运行合并排序对矢量字符串进行排序并打印输出。

int main(int argc, char** argv) {
string input;
while(getline(cin, input)){
vector<string> v;
//Shift string to the left by 1 character and add it to the vector
for(int i=0; i< input.length(); i++)
{
v.push_back(input);
rotate(input.begin(),input.begin()+1,input.end());
}

//  Print original vector
cout << "******original*******"<< endl;
for (vector<string>::size_type i = 0; i < v.size(); ++i)
{
cout << v[i] << endl;
}

//Do the mergesort
mergeSort(v, 0, v.size() - 1);
//print output
cout << "*****sorted******"<< endl;
for (vector<string>::size_type i = 0; i < v.size(); ++i)
{
cout << v[i] << endl;
}
} //end while loop
}
void mergeSort(vector<string> &a, int from, int to)
{
if (from == to)
{
return;
}
int mid = (from + to) / 2;
mergeSort(a, from, mid);
mergeSort(a, mid + 1, to);
merge(a, from, mid, to);
} 
void merge(vector<string> &a, int from, int mid, int to)
{
int n = to - from + 1; 
vector<string> b(n); 
int i1 = from; 
int i2 = mid + 1;
int j = 0; /
while (i1 <= mid && i2 <= to)
{
if (a[i1].compare(a[i2]) < 0)
{
b[j] = a[i1];
i1++;
}
else
{
b[j] = a[i2];
i2++;
}
j++;
}
while (i1 <= mid)
{
b[j] = a[i1];
i1++;
j++;
}
while (i2 <= to)
{
b[j] = a[i2];
i2++;
j++;
}
for (j = 0; j < n; j++)
{
a[from + j] = b[j];
}
} 

我的预期输出应该是:

******original*******
Hello
elloH
lloHe
loHel
oHell
******sorted*******
Hello
elloH
lloHe
loHel
oHell
******original*******
world.
orld.w
rld.wo
ld.wor
d.worl
.world
******sorted*******
.world
d.worl
ld.wor
orld.w
rld.wo
world.
******original*******
******sorted*******
******original*******
3 days of summer!
days of summer!3
days of summer!3
ays of summer!3 d
ys of summer!3 da
s of summer!3 day
of summer!3 days
of summer!3 days
f summer!3 days o
summer!3 days of
summer!3 days of
ummer!3 days of s
mmer!3 days of su
mer!3 days of sum
er!3 days of summ
r!3 days of summe
!3 days of summer
******sorted*******
days of summer!3
of summer!3 days
summer!3 days of
!3 days of summer
3 days of summer!
ays of summer!3 d
days of summer!3
er!3 days of summ
f summer!3 days o
mer!3 days of sum
mmer!3 days of su
of summer!3 days
r!3 days of summe
s of summer!3 day
summer!3 days of
ummer!3 days of s
ys of summer!3 da

问题是,当文本文件中有一个空白换行符时,排序就会停止。这是我得到的输出:

******original******* 
Hello 
elloH 
lloHe 
loHel 
oHell
******sorted******* 
Hello 
elloH 
lloHe 
loHel 
oHell
******original******* 
world. 
orld.w 
rld.wo 
ld.wor 
d.worl 
.world
******sorted******* 
.world 
d.worl 
ld.wor 
orld.w 
rld.wo 
world.
******original*******

似乎出了什么问题?

当你的向量为空时,你传递给mergeSortv.size() - 1变成一个大的正数(size_tv.size()返回是无符号的(。然后将其转换为可能导致-1int。这最终将导致merge内部的某个地方超出向量的范围。由于to小于from您也很可能最终得到无限递归。

最简单的解决方法是在向量少于 2 个元素时不排序:

if ( v.size >= 2 )
{
mergeSort(v, 0, v.size() - 1);
}