简体   繁体   English

C ++中Mergesort的问题

[英]Problem with Mergesort in C++

vector<int>& mergesort(vector<int> &a) {
    if (a.size() == 1) return a;
    int middle = a.size() / 2;
    vector<int>::const_iterator first = a.begin();
    vector<int>::const_iterator mid = a.begin() + (middle - 1);
    vector<int>::const_iterator last = a.end();
    vector<int> ll(first, mid);
    vector<int> rr(mid, last);

    vector<int> l = mergesort(ll);
    vector<int> r = mergesort(rr);
    vector<int> result;
    result.reserve(a.size());
    int dp = 0, lp = 0, rp = 0;

    while (dp < a.size()) {
        if (lp == l.size()) {
            result[dp] = (r[rp]);
            rp++;
        } else if (rp == r.size()) {
            result[dp] = (l[lp]);
            lp++;
        } else if (l[lp] < r[rp]) {
            result[dp] = (l[lp]);
            lp++;
        } else {
            result[dp] = (r[rp]);
            rp++;
        }
        dp++;
    }
    a = result;
    return a;
}

It compiles correctly but while execution, I am getting: 它正确编译但在执行时,我得到:

This application has requested the runtime to end it in an unusual way. 此应用程序已请求运行时以不寻常的方式结束它。

This is a weird error. 这是一个奇怪的错误。

Is there something that is fundamentally wrong with the code? 这些代码是否存在根本错误?

This result.reserve(a.size()) just affects the vector's capacity , not it's size . 这个result.reserve(a.size())只影响向量的容量 ,而不是它的大小 (The vector's capacity tells up to which size the vector can grow without needing to re-allocate and copy all members. Basically it's only there for optimization purposes.) You cannot access any members in result after that reservation, since there aren't any. (向量的容量告诉向量可以增长到哪个大小而无需重新分配和复制所有成员。基本上它只用于优化目的。)在预留之后你不能访问result任何成员,因为没有任何成员。 Either use result.push_back(...) instead of result[dp] = ... or result.resize(a.size()) instead of result.reserve(a.size()) . 使用result.push_back(...)而不是result[dp] = ...result.resize(a.size())而不是result.reserve(a.size())

I suppose the former could be more effective. 我想前者可能更有效。

One problem is with the usage of reserve() (either use resize() or append items with push_back() instead of accessing the index). 一个问题是使用reserve() (使用resize()或使用push_back()附加项而不是访问索引)。


if (a.size() == 1) return a;
int middle = a.size() / 2;
vector<int>::const_iterator first = a.begin();
vector<int>::const_iterator mid = a.begin() + (middle - 1);
vector<int>::const_iterator last = a.end();
vector<int> ll(first, mid);
vector<int> rr(mid, last);

This could be another problem. 这可能是另一个问题。 If the size is 2, then ll would end up being an empty vector, and this function doesn't appear to handle this. 如果大小为2,则ll最终将成为空向量,并且此函数似乎不会处理此问题。 There doesn't seem to be much reason to subtract 1 from middle anyway. 无论如何,似乎没有太多理由从中间减去1。


It is also possible that you are copying things around far more than needed: you shouldn't need the l and r vectors (because they will just be copies of ll and rr ), similarly I don't think you need the result vector, since you could just write the merged results right back to a . 也有可能你复制的东西远远超过需要:你不应该需要lr向量(因为它们只是llrr副本),同样我认为你不需要result向量,因为你可以只写结果合并后马上回a

reserve() does not change the amount of contents in the vector. reserve()不会更改向量中的内容量。 You've created an empty vector, reserved a certain size for it, and then used v[i] = x; 你已经创建了一个空向量,为它保留了一定的大小,然后用v [i] = x; This is not a valid use of vector since there still isn't any i'th element in the vector. 这不是向量的有效使用,因为向量中仍然没有任何第i个元素。

Use resize() instead. 请改用resize()。

The error message is very unclear, because it is the standard message of VC++ when an exception goes unhandled. 错误消息非常不清楚,因为当异常未处理时,它是VC ++的标准消息。 You could add a try-catch-clock to your main to get the exception and a more meaningful error: 您可以在main中添加try-catch-clock以获取异常并产生更有意义的错误:

int main() {
    try {
        // do main stuff here
    }
    catch ( std::exception & e ) {
        std::cout << e.what() << std::endl;
    }
}

This is for the command-line. 这是针对命令行的。 If your application doesn't have one, use a message box or write the error to a log-file. 如果您的应用程序没有,请使用消息框或将错误写入日志文件。

With regard to the problem in your code, all the other answers appear to be correct. 关于代码中的问题,所有其他答案似乎都是正确的。

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

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