C++ 使用c+中的向量解决问题+;显示𝒓𝒖𝒏𝒕𝒊𝒎𝒆𝒆𝒓𝒓𝒐𝒓;提交时,但在使用自定义输入运行相同输入时运行良好

C++ 使用c+中的向量解决问题+;显示𝒓𝒖𝒏𝒕𝒊𝒎𝒆𝒆𝒓𝒓𝒐𝒓;提交时,但在使用自定义输入运行相同输入时运行良好,c++,vector,error-handling,runtime-error,out-of-memory,C++,Vector,Error Handling,Runtime Error,Out Of Memory,最近我在interviewbit.com上解决了这个问题 我使用向量解决了这个问题,并对向量执行了操作 这是我的密码: vector<Interval> Solution::insert(vector<Interval> &intervals, Interval newInterval) { int n=intervals.size(),leftfound=0,rightfound=0,count=0; if(n==0)

最近我在interviewbit.com上解决了这个问题

我使用向量解决了这个问题,并对向量执行了操作

这是我的密码:

    vector<Interval> Solution::insert(vector<Interval> &intervals, Interval newInterval)
    {

    int n=intervals.size(),leftfound=0,rightfound=0,count=0;
    if(n==0)                  //for the case when intervals vector is empty
       {  
        intervals.push_back(newInterval);
        return intervals;   
       }

    int t=0;
    if(newInterval.end<newInterval.start)   //if(start>end) swap
       { 
        t=newInterval.start;
        newInterval.start=newInterval.end;
        newInterval.end=t;
       }
    if(newInterval.start>intervals[n-1].end) //if the newInterval succedes every other
       {
        intervals.insert(intervals.end(),newInterval);
        return intervals;
       }
    if(newInterval.end<intervals[0].start)//if the newInterval precedes every other element
       {
        intervals.insert(intervals.begin(),newInterval);
        return intervals;
       }

    auto left=intervals.begin(),right=intervals.begin(); //just initialising with something
    auto it=intervals.begin() ; // iterator for loops

    while((*it).start<newInterval.start&&it!=intervals.end()) //*it is dereferencing the iterator to 
        it++;         //get the element of vector "intervals" at that index

    it--; // decrementing it to reach the desired interval

    if((*it).start<=newInterval.start&&(*it).end>=newInterval.start)
       {
        leftfound=1;left=it;
       }
    else 
        left=it+1;

    it=left;

    while((*it).end<newInterval.end&&it!=intervals.end())
        it++;

    if((*it).start<=newInterval.end&&(*it).end>=newInterval.end)
       { 
        rightfound=1; right=it;
       }
    else 
        right=it-1;


    if(right-left==-1&&leftfound==0&&rightfound==0)// this if will be true in cases like:
        intervals.insert(left,newInterval);          //intervals=[(1,2),(8,10)] and newInterval=(4,6)
    else        //in every other case this else will execute
       {   
        if(leftfound==0)
        (*left).start=newInterval.start;
        if(rightfound==0)
        (*left).end=newInterval.end;
        else (*left).end=(*right).end;
       }
    left=left+1;

    intervals.erase(left,right+1);
    return intervals;
}
现在,当使用站点上的测试按钮进行测试时,此解决方案运行良好。之后,当我按submit时,会显示以下错误: . 当我使用自定义输入运行相同的测试用例(遇到错误)时,代码运行良好,并给出预期的输出。我似乎找不到问题所在。我怀疑这可能是因为erase函数,因为在错误中它显示了
free()
,但我甚至不能确定

另外,我在代码中添加了结构和主函数,运行起来非常好

intervals.insert(left,newInterval); 
std::vector::insert
使向量内容的所有现有迭代器无效。紧接着:

left=left+1;

intervals.erase(left,right+1);
同一向量的
迭代器此时不再有效,使用它们是未定义的行为

这可能是也可能不是所示代码中的唯一错误。破损的缩进使显示的代码难以阅读和理解;但这是所示代码中的一个明确错误,这可能是未定义行为和崩溃的原因

std::vector::insert
使向量内容的所有现有迭代器无效。紧接着:

left=left+1;

intervals.erase(left,right+1);
同一向量的
迭代器此时不再有效,使用它们是未定义的行为


这可能是也可能不是所示代码中的唯一错误。破损的缩进使显示的代码难以阅读和理解;但这是所示代码中的一个明确错误,这可能是未定义行为和崩溃的原因。

我接受了您的建议并修复了缩进。我试图理解您的答案,并对其进行了一些理解,因为我不是很好,我有以下跟踪和疑问:问题的哪一部分“std::vector::insert使所有现有迭代器无效”您不清楚吗?这并没有什么复杂的。在
insert()之后
ing进入向量你不能使用任何现有的迭代器进入向量,因为它们不再有效。仅此而已。显示的代码继续使用它们,因此可能会崩溃。好的,我明白了。现在请打开这个pastebin.com/GtCkqm61。我有疑问,不用担心它们不是因为我不理解你的答案我接受了你的建议并修正了缩进。我试图理解你的答案,并且理解了它,因为我不是很好,我有以下的跟进和疑问:你不清楚“std::vector::insert使所有现有迭代器无效”的哪一部分?这没有什么复杂的。在
insert()之后
ing进入向量你不能使用任何现有的迭代器进入向量,因为它们不再有效。仅此而已。显示的代码继续使用它们,因此可能会崩溃。好的,我明白了。现在请打开这个pastebin.com/GtCkqm61。我有疑问,不用担心它们不是因为我不理解你的答案