繁体   English   中英

使用矢量时找不到错误发生的位置

[英]I can't find where the error occurred when using vector

当我运行此代码时,我会收到 Debug Assertion Failed 错误。

“表达式:向量擦除迭代器超出范围”

我找不到错误发生的位置。

for (int i = 0; i < comb.size(); i++) {
        if (couple.size() != 0 && couple.size() == mate * 2) {
            vector<int>::iterator iter = couple.begin();
            int rad = rand() % couple.size();
            rad = (rad % 2 == 0 ? rad : rad + 1);
            iter += rad;
            iter = couple.erase(iter);
            iter = couple.erase(iter);
        }
        couple.push_back(comb[i]);
        printf("%d ", comb[i]);
    }
  • 让我们假设couple.size() == 6
  • 让我们假设rad == 5
  • 然后rad将转换为 6。
  • 然后,您将在数组末尾之后的迭代器上调用 erase()。 这是非法的。

我猜你应该这样做:

rad = (rad % 2 == 0 ? rad : rad - 1);

但很难说,因为您从未解释过您要完成的工作。

暂无
暂无

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

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