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

标签 c++ vector error-handling runtime-error out-of-memory

最近我在 interviewbit.com 上解决这个问题 - https://www.interviewbit.com/problems/merge-intervals/ .

我使用 vector 和对 vector 执行操作解决了这个问题

这是我的代码:

    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;
}

interviewbit.com平台(IDE)要求只完成题中给出的功能。

NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.

我需要实现这个:

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

Interval 结构也定义为:

struct Interval {
    int start;
    int end;
    Interval() : start(0), end(0) {}
    Interval(int s, int e) : start(s), end(e) {}
};

现在,当使用站点上的测试按钮进行测试时,此解决方案运行良好。之后,当我按提交时,它显示以下错误: click to see error .当我使用自定义输入运行相同的测试用例(遇到错误)时,代码运行正常并给出预期的输出。我似乎找不到问题所在。我怀疑这可能是因为删除功能,因为在错误中它显示 free() , 但我一点也不确定。

另外,我在 ideone.com 上运行了代码在代码中添加结构和主要功能,它运行得非常好。

最佳答案

intervals.insert(left,newInterval); 

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

left=left+1;

intervals.erase(left,right+1);

指向同一 vector 的 leftright 迭代器此时不再有效,使用它们是未定义的行为。

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

关于c++ - 在 C++ 中使用 vector 的问题的解决方案在提交时显示 𝒓𝒖𝒏𝒕𝒊𝒎𝒆 𝒆𝒓𝒓𝒐𝒓 但在使用自定义输入运行相同输入时运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59249355/

相关文章:

c++ - 创建 vector 序列 C++

c++ - STL/Boost 相当于 LLVM SmallVector?

swift - 错误: Cannot convert value type GameScene to expected argument SKScene

c++ - 是否有任何 ATL 支持的宏来检查 hresults 并返回值?

c++ - 循环和内联函数

java - JNA 无效的内存访问

c++ - QML 无法识别 C++ 函数

c++ - “expected ')' before textIn”和“expected constructor, destructor or type conversion before '(' token”

c++ - For 循环遍历生长 vector

php - PHP 中的异常 - set_exception_handler 输出