[英]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.