简体   繁体   English

合并排序中的C ++向量中的std :: bad_alloc错误

[英]std::bad_alloc error in C++ vector in merge sort

I am trying to implement merge sort using vector in c++, i am getting following error - terminate called after throwing an instance of 'std::bad_alloc'. 我试图在c ++中使用向量实现合并排序,我得到以下错误 - 在抛出'std :: bad_alloc'的实例后调用terminate。 There were other solutions for bad_alloc but it didn't help. bad_alloc还有其他解决方案,但没有帮助。

problem is merging function- 问题是合并功能 -

#include<iostream>
#include<vector>
using namespace std;

void print(vector<int> v)
{
    for(int i=0; i<v.size(); i++)
        cout << v[i] << " ";
    cout << endl;
}

vector<int> merging(vector<int> left, vector<int> right)
{
    vector<int> result;
    while((int)left.size()>0 && (int)right.size()>0)
    {
        if((int)left.front()<=(int)right.front()){
            result.push_back(left.front());
            //left.erase(left.begin());
        }
        else{
            result.push_back(right.front());
            //left.erase(right.begin());
        }
    }
        while((int)left.size()>0){
            for(int j=0; j<(int)left.size(); j++)
                result.push_back(left[j]);
        }
        while((int)right.size()>0){
            for(int k=0; k<(int)right.size(); k++)
                result.push_back(right[k]);
        }
    cout << "check merging";
    return result;
}

int main()
{
    vector<int> a, b, result;
    a.push_back(38);
    a.push_back(27);
    b.push_back(43);
    b.push_back(3);
    cout << a.front() << endl;
    print(a);
    cout << endl;
    print(b);
    cout << endl;
    result = merging(a, b);
    print(result);
}

thank you 谢谢

From the page on std::bad_alloc std::bad_alloc上的页面

Type of the exceptions thrown by the standard definitions of operator new and operator new[] when they fail to allocate the requested storage space. 当operator new和operator new []的标准定义未能分配所请求的存储空间时抛出的异常类型。

You're running out of memory and an exception is being thrown when you can't allocate anymore. 你的内存不足,当你不能再分配时会抛出异常。

Try taking a look at the exit conditions for your loops. 尝试查看循环的退出条件。

while((int)left.size()>0 && (int)right.size()>0)

This loop won't exit until both left and right are empty, however you're never changing their size (the erase calls are commented out). leftright都为空之前,此循环不会退出,但是您永远不会更改它们的大小( erase调用已被注释掉)。

The other two while loops have similar issues. 另外两个while循环有类似的问题。

The first while loop never terminates. 第一个while循环永远不会终止。 It keeps adding elements to result until the memory is exhausted. 它会不断向结果中添加元素,直到内存耗尽为止。

There are other issues with your code: In the case of left.front() <= right.front you never remove the element you put in result from left, it is commented out. 你的代码还有其他问题:在left.front()<= right.front的情况下你永远不会删除你从左边放入的元素,它会被注释掉。 In that else, you do not remove from right either. 在那个其他地方,你也不会从right移除。 The commented out code would remove from left , mixing an iterator from right into an erase from left , what is technically called "undefined behavior" meaning that your program is toast. 注释掉的代码将从left移除,将right的迭代器混合到left的擦除中,技术上称为“未定义的行为”意味着您的程序是吐司。

Also, why do you cast to int everywhere? 另外,为什么你到处都是int? that's so unnecessary 那太没用了

Even if you would erase from the front of left and right, you would use vectors for removing from the front, operations that are not supported efficiently by a vector. 即使你要从左前方和右前方擦除,你也可以使用向量从前面移除,这些操作不是由向量有效支持的。 There is no need to do that, you can traverse left and right using iterators, checking instead of whether they are empty for whether the left iterator reached the end of left and whether the right iterator reached the end of right 有没有需要做的是,你可以遍历leftright使用迭代器,而不是检查他们是否是空的左迭代器是否达到结束left以及是否正确到达迭代的末尾right

You have an infinite loop. 你有一个无限循环。

while((int)left.size()>0 && (int)right.size()>0)
{
    if((int)left.front()<=(int)right.front()){
        result.push_back(left.front());
        //left.erase(left.begin());
    }
    else{
        result.push_back(right.front());
        //left.erase(right.begin());
    }
}

Is never going to end as you never remove any elements from left 永远不会结束,因为你永远不会从left删除任何元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM