合并排序代码中的分段错误

我已经用C ++编写了此合并排序程序,但是在运行代码后出现“分段错误(内核已转储)”错误。即使没有编译错误。您能告诉我我在做什么错吗?在数组中输入数据时,它显示该错误。如果我将其更改为push_back,则输入很好,但是稍后在合并功能中,它显示相同的错误。


//merging 2 sorted subarrays.

#include <iostream>

#include <vector>

using namespace std;


void merge(vector <int> &a,vector <int> &b,vector <int> &c)

{

    int i=0,j=0,k=0,bL=b.size(),cL=c.size();

    while(i<bL && j<cL)

    {

        if(b[i]<c[j])

        {

            a[k]=b[i];

            i++;k++;

        }

        else

        {

            a[k]=c[j];

            j++;k++;

        }

    }

    while(i<bL)

    {

        a[k]=b[i];

        i++;k++;

    }

    while(j<cL)

    {

        a[k]=c[j];

        j++;k++;

    }

    cout<<"array a inside merge is: "<<endl;

    for(int p=0;p<a.size();p++)

    {

        cout<<a[p]<<endl;

    }


}

void mergeSort(vector <int> &a)

{

    vector <int> l, r;

    int mid;

    if(a.size()<2) return;


    mid = a.size()/2;

    for(int i=0;i<mid;i++)

    {

        l[i]=a[i];

    }

    for(int i=mid;i<a.size();i++)

    {

        r[i-mid]=a[i];

    }

    mergeSort(l);

    mergeSort(r);

    merge(a, l, r);

}

int main()

{

    int n;

    vector <int> a;

    cin>>n;

    for(int i=0;i<n;i++)

    {

        cin>>a[i];

    }

    mergeSort(a);

    for(int p=0;p<n;p++)

    {

        cout<<a[p]<<endl;

    }

    return 0;

}


慕妹3242003
浏览 294回答 3
3回答

小唯快跑啊

如果您使用向量访问向量中的元素,则[]可能会遇到段错误。这段代码,vector&nbsp;<int>&nbsp;a;给你一个空向量。a[0]如果什么都没有,问问就行不通a。尝试将其设置a[0]为一个值也不起作用。它不存在。然而。您在mergeSort使用时遇到类似的问题vector&nbsp;<int>&nbsp;l,&nbsp;r;这些也是空向量。您可以使用push_back(甚至emplace_back)添加新元素。或使用构造函数重载来声明所需的元素数量。例如,vector&nbsp;<int>&nbsp;a(10);给您一个十个整数的向量,因此a[0]可以读取或写入。&nbsp;a[11]不是。首先练习使用向量,然后尝试合并排序。

繁星点点滴滴

出现分段错误的原因是您访问了不存在的内存位置(更准确地说是未分配)。假设您有一个长度为3的向量,并且尝试访问第4个位置,则会遇到分割错误。与@doctorlove的答案相反,我想说可以使用[]。但是,您需要以下实现(仅针对main(),请在其他功能中以相同的逻辑实现)。有关std::vector更多信息,请参见的文档。int main(){&nbsp; &nbsp; size_t n;&nbsp; &nbsp; std::cin >> n;&nbsp; &nbsp; std::vector <int> a(n);&nbsp; &nbsp; for(int i=0;i<n;++i)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; std::cin >> a[i];&nbsp; &nbsp; }&nbsp; &nbsp; // mergeSort(a);&nbsp; &nbsp; for(int i=0;i<n;++i)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; std::cout << a[i] << "\n";&nbsp; &nbsp; }&nbsp; &nbsp; return 0;}希望这可以帮助。干杯。

富国沪深

这是更改后的最终代码://merging 2 sorted subarrays.#include <iostream>#include <vector>using namespace std;void merge(vector <int> &a,vector <int> &b,vector <int> &c){&nbsp; &nbsp; int i=0,j=0,k=0,bL=b.size(),cL=c.size();&nbsp; &nbsp; while(i<bL && j<cL)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; if(b[i]<c[j])&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; a[k]=b[i];&nbsp; &nbsp; &nbsp; &nbsp; i++;k++;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; a[k]=c[j];&nbsp; &nbsp; &nbsp; &nbsp; j++;k++;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; while(i<bL)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; a[k]=b[i];&nbsp; &nbsp; &nbsp; i++;k++;&nbsp; &nbsp; }&nbsp; &nbsp; while(j<cL)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; a[k]=c[j];&nbsp; &nbsp; &nbsp; j++;k++;&nbsp; &nbsp; }&nbsp; &nbsp; cout<<"array a inside merge is: "<<endl;&nbsp; &nbsp; for(int p=0;p<a.size();p++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; cout<<a[p]<<endl;&nbsp; &nbsp; }}void mergeSort(vector <int> &a){&nbsp; &nbsp; vector <int> l, r;&nbsp; &nbsp; int mid;&nbsp; &nbsp; if(a.size()<2) return;&nbsp; &nbsp; mid = a.size()/2;&nbsp; &nbsp; for(int i=0;i<mid;i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; l.push_back(a[i]);&nbsp; &nbsp; }&nbsp; &nbsp; //change2&nbsp; &nbsp; for(int i=0;i<a.size()-mid;i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; r.push_back(a[mid+i]);&nbsp; &nbsp; }&nbsp; &nbsp; mergeSort(l);&nbsp; &nbsp; mergeSort(r);&nbsp; &nbsp; merge(a, l, r);}int main(){&nbsp; &nbsp; int n;&nbsp; &nbsp; cin>>n;&nbsp; &nbsp; //change1&nbsp; &nbsp; vector <int> a(n);&nbsp; &nbsp; for(int i=0;i<n;i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; cin>>a[i];&nbsp; &nbsp; }&nbsp; &nbsp; mergeSort(a);&nbsp; &nbsp; cout<<"Final array is:"<<endl;&nbsp; &nbsp; for(int p=0;p<n;p++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; cout<<a[p]<<endl;&nbsp; &nbsp; }&nbsp; &nbsp; return 0;}
打开App,查看更多内容
随时随地看视频慕课网APP