递归插入排序中出现分段错误
我试图编写插入排序的递归代码,但出现分段错误。请帮我解决这个问题。
#include <bits/stdc++.h>
using namespace std;
void insert(vector<int> &v,int temp){
if(v.size()==0||v[v.size()-1]<=temp){
v.push_back(temp);
return;
}
int val = v[v.size()-1];
v.pop_back();
insert(v, temp);
v.push_back(val);
return;
}
void sort(vector<int> &v){
if(v.size()==1) return;
int temp = v[v.size()-1];
v.pop_back();
sort(v);
insert(v,temp);
}
int main() {
int n;
vector<int> v;
cin>>n;
for(int i=0;i<n;i++)
cin>>v[i];
sort(v);
for(int i=0;i<n;i++)
cout<<v[i];
return 0;
}
I was trying to write recursive code of insertion sort, but I am getting segmentation fault. Please help me with this.
#include <bits/stdc++.h>
using namespace std;
void insert(vector<int> &v,int temp){
if(v.size()==0||v[v.size()-1]<=temp){
v.push_back(temp);
return;
}
int val = v[v.size()-1];
v.pop_back();
insert(v, temp);
v.push_back(val);
return;
}
void sort(vector<int> &v){
if(v.size()==1) return;
int temp = v[v.size()-1];
v.pop_back();
sort(v);
insert(v,temp);
}
int main() {
int n;
vector<int> v;
cin>>n;
for(int i=0;i<n;i++)
cin>>v[i];
sort(v);
for(int i=0;i<n;i++)
cout<<v[i];
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将创建一个空的
vector
:访问其中的任何元素将使您的程序具有 未定义行为。
您可以通过向构造函数提供
n
或调用resize(n)
来创建具有所需元素数量的vector
> 创建空的向量
后。另一种选择是将其创建为空,然后到push_back()< /code>
或
emplace_back
元素放入其中,在这种情况下它将动态增长。使用正确数量的元素创建它的示例:
演示
注意:
#include < ;bits/stdc++.h>
- 切勿包含此非标准标头。包含标准标头,例如iostream
和vector
using namespace std;
- 不要这样做。尤其是当您为sort
等函数提供重载时,如果您包含bits/stdc++.h
并执行using namespace std;
,这些函数就可用。This creates an empty
vector<int>
:Accessing any elements in it will make your program have undefined behavior.
You can instead create the
vector<int>
with the number of elements you want by supplyingn
to the constructor, or by callingresize(n)
after creating an emptyvector<int>
. Another option is to create it empty, and then topush_back()
oremplace_back
elements into it in which case it'll grow dynamically.Example creating it with the correct number of elements:
Demo
Notes:
#include <bits/stdc++.h>
- Never include this non-standard header. Include the standard headers, likeiostream
andvector
using namespace std;
- Don't do this. Especially not when you provide overloads for functions likesort
that will be available if you includebits/stdc++.h
and dousing namespace std;
.问题是向量
v
是空(意味着它的大小0
),而你试图访问其元素会导致未定义的行为。在上面的代码片段中,向量
v
是一个空向量,意味着它没有元素。所以表达式cin >> v[i]
会导致未定义的行为,因为您正在访问向量的第 i 个索引处的元素,但向量内没有要访问的元素。因此,您看到的(也许看到的)输出是未定义行为的结果。正如我所说,不要依赖具有 UB 的程序的输出。该程序可能会崩溃(这就是您的情况)。
因此,使程序正确的第一步是删除 UB。 只有那时您才能开始推理程序的输出。
解决方案
要解决这个问题,您可以在创建向量时指定向量的大小,如下所示:
1有关未定义行为的更准确的技术定义,请参阅这里其中提到:对程序。
The problem is that the vector
v
is empty(meaning it has size0
) and you're trying to access its elements which leads to undefined behavior.In the above snippet, the vector
v
is an empty vector meaning it has no elements. So the expressioncin >> v[i]
, leads to undefined behavior because you're accessing elements ati
th index of the vector but there are no elements inside the vector to access.So the output that you're seeing(maybe seeing) is a result of undefined behavior. And as i said don't rely on the output of a program that has UB. The program may just crash(which is what happens in your case).
So the first step to make the program correct would be to remove UB. Then and only then you can start reasoning about the output of the program.
Solution
To solve this you can specify the size of the vector when creating it as shown below:
1For a more technically accurate definition of undefined behavior see this where it is mentioned that: there are no restrictions on the behavior of the program.
您声明了一个空向量
,因此在此 for 循环中使用下标运算符
会调用未定义的行为。
相反,您可以在输入
n
的值后首先使用n
元素声明向量,例如,或者您可以在输入
n< 的值之后调整向量的大小/code> 变得像 At 一样,
最好将变量 n 声明为具有无符号整数类型,例如,
您可以使用基于范围的 for 循环代替 for 循环
在函数
sort
中,您 可以使用基于范围的 for 循环。需要至少按以下方式更改 if 语句中的条件,因为通常用户可以传递一个空向量。
您可以使用成员函数
back
,而不是使用像v[v.size()-1]
这样的表达式。例如,这使代码更具可读性。
我将按以下方式定义函数,而不使用多余的返回语句
这是一个演示程序。
程序输出是
建议:尽量避免使用 using 指令
,它可能是名称冲突的原因,有时会使代码不清楚是否使用了标准 C++ 函数或用户定义的函数。
You declared an empty vector
So using the subscript operator in this for loop
invokes undefined behavior.
Instead you could either initially declare the vector with
n
elements after the value ofn
was entered as for exampleOr you could resize the vector after the value of
n
becomes known likeAt it would be better to declare the variable n as having an unsigned integer type as for example
Instead of the for loop you can use the range based for loop
Within the function
sort
you need to change the condition in the if statement at least the following waybecause in general the user can pass an empty vector.
Instead of using expressions like this
v[v.size()-1]
you could use the member functionback
. For examplethat makes the code more readable.
I would define the functions the following way without redundant return statements
Here is a demonstration program.
The program output is
And advice: try to avoid the using directive
it can be a reason of name collisions and sometimes makes the code unclear that is whether there is used a standard C++ function or a user defined function.