C++ 换行符上的合并排序错误中断
C++ Merge sorting error breaks on newline text
我正在对字符串向量进行合并排序。到目前为止,我在函数中实现了大部分算法。
我使用<输入从命令提示符获取输入.txt将输入从文本文件传递到>output.txt 输出。输入从命令提示符获取输入.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*******
似乎出了什么问题?
当你的向量为空时,你传递给mergeSort
v.size() - 1
变成一个大的正数(size_t
从v.size()
返回是无符号的(。然后将其转换为可能导致-1
的int
。这最终将导致merge
内部的某个地方超出向量的范围。由于to
小于from
您也很可能最终得到无限递归。
最简单的解决方法是在向量少于 2 个元素时不排序:
if ( v.size >= 2 )
{
mergeSort(v, 0, v.size() - 1);
}
相关文章:
- 将图形表示为unordered_map<字符串、向量>时拓扑排序错误<string>
- C++插入排序错误功能不起作用
- C++ 换行符上的合并排序错误中断
- C++ 快速排序错误
- 合并排序错误 - 表达式必须具有常量值
- C 插入排序错误
- 对向量 OS 字符串进行排序 C++ 错误 C2784 无法推断模板参数
- 递归合并排序错误
- 使用C++的快速排序错误
- std::使用自定义类排序错误
- 使用类在 C++ 中快速排序错误
- 数组中的函数排序错误
- 插入排序错误
- C++部分排序错误
- sort对某些值排序错误
- 针对c++ STL的自定义比较器排序错误预期-在')'标记之前的主表达式
- 气泡排序错误LNK2019
- 链表的归并排序错误
- 快速排序错误:变量周围的堆栈已损坏
- 排序错误:不允许使用类型名称