[英]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。我猜你应该这样做:
rad = (rad % 2 == 0 ? rad : rad - 1);
但很难说,因为您从未解释过您要完成的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.