我想遍历一个列表,然后,如果对象的板号与通过参数给出的板号相匹配,并且收费(以收费()计算)小于或等于给定的分,从列表中删除/删除对象。我不断收到列表迭代器不能增加的错误,我对如何解决这个问题毫无头绪。C++ |列表迭代器不可递增
void one_time_payment(string& plate_number, int cents) {
// TODO: REWRITE THIS FUNCTION
std::list<LicenseTrip>:: iterator it;
for (it = listLicense.begin(); it != listLicense.end(); std::advance(it, 1)) {
if (it->plate_number().compare(plate_number) == 0) {
cout << "Matching Plate Found" << endl;
if (it->toll() <= cents) {
cout << "Can be paid" << endl;
it = listLicense.erase(it); //Error: list iterator cannot be incremented
}
}
}
cout << "End of Iterator" << endl;
}
取代'它= listLicense.erase(它);''与listLicense.erase(它);' –
(HTTP [未能与GCC重现]:// melpon.org/wandbox/permlink/4ADAdPjUCnV3cthh)。请发布[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 – MikeCAT
无法用gcc重现。此外,代码中存在一个错误。如果“if”条件返回true,则会导致未定义的行为。在这种情况下,erase()将返回end(),这将被分配给'it',并且循环迭代再次递增'it'。这是未定义的行为。 –