简体   繁体   English

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

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

When I run this code, I can get the Debug Assertion Failed error.当我运行此代码时,我会收到 Debug Assertion Failed 错误。

"Expression: vector erase iterator outside range" “表达式:向量擦除迭代器超出范围”

I can't find where the error occurred.我找不到错误发生的位置。

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]);
    }
  • Let's assume couple.size() == 6 .让我们假设couple.size() == 6
  • Let's assume rad == 5 .让我们假设rad == 5
  • Then rad will be transformed to 6.然后rad将转换为 6。
  • Then you will call erase() on an iterator past the end of the array.然后,您将在数组末尾之后的迭代器上调用 erase()。 Which is illegal.这是非法的。

I'm going to guess that you should have done:我猜你应该这样做:

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

But it's hard to say since you never explained what you're trying to accomplish.但很难说,因为您从未解释过您要完成的工作。

暂无
暂无

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

相关问题 为什么在向量上使用 std::find() 时会出错? - Why do I get an error when using std::find() on a vector? 在向量使用中找不到致命错误 - Can't find fatal error in vector use 当我使用父类指针将它们添加到向量时,我不能使用子类特定的函数 - I can't use subclass specific funcitons when I add them to a vector using parent class pointers 如果我使用 std:find 在向量对中找到一个元素,如何将向量中的值转换为字符串? - If I find an alement in a vector pairs using std:find, how can I get the values from the vector into a string? 不止一个重载 function 实例与参数列表匹配,我找不到错误发生的位置 - More than one instance of overloaded function matches the argument list and I can't find where the error happens 在向量C ++上使用find()时发生编译器错误 - Compiler Error when using find() on a vector C++ 每当我尝试使用 find() 将字符串添加到向量时出错 - Error whenever I try to add a string to a vector using find() 无法找到此 std::logic_error 的来源 - Can't find where this std::logic_error is coming from 编译以下代码时发生错误 - Error occurred when I compile the following code Shakersort c++,错误:从 'int' 到 'int* 的无效转换 - 我找不到混合 integer 和指向整数的指针的位置 - Shakersort c++, error: invalid conversion from ‘int’ to ‘int* - I can't find where I mixed integer and pointer to integers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM