我正在尝试在 C++ 中对向量进行插入排序,但我的 ./a.out 返回:* `./insertion' 中的错误:双重释放或损坏(输出):0x000000000154fc20 *
不知道为什么会发生这种情况,我见过类似的其他问题,而且总是与代码有关,购买我的代码我不知道问题是什么。
这是代码:
#include<iostream>
#include<vector>
using namespace std;
void insertion(vector<int> v){
int tam = v.size();
int key,j,i;
for(i=1; i<tam; i++){
key = v[i];
j=i-1;
while(j>=0 && v[j]>key){
v[j+1] = v[j];
j--;
}
v[j]=key;
}
}
void print(vector<int> v){
cout<<endl;
for(int i = 0; i<v.size(); i++){
cout<<i+1<<".\t"<<v[i]<<"\n";
}
}
int main(){
cout<<"----------------INSERTION SORT----------------\n\n";
cout<<"\nPlease, fill the vector: \n\n";
vector<int> v;
int a;
bool response = true;
while(response){
cout<<"\nEnter your number: ";
cin>>a;
v.push_back(a);
cout<<"Another?(1/0): ";
cin>>response;
cout<<endl;
}
insertion(v);
print(v);
return 0;
}