[英]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]);
}
couple.size() == 6
.couple.size() == 6
。rad == 5
.rad == 5
。rad
will be transformed to 6.rad
将转换为 6。 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.