So I am seg faulting when I run this function
class vector <Record<value> >::iterator itr = records.begin();
for (; itr != records.end(); ++itr) {
if (itr->isSelected()) {
itr = records.erase(itr);
recordSize--;
}
}
where my vector is of vector <Record <value> > records;
and the function isSelected()
is just a boolean
that is either true when the object is selected or false when its not.
Can someone help me please, I don't see the problem with doing it this way
In the case where you're deleting the last element, itr
will first be records.end()
because that's what records.erase()
will return, and then you're incrementing it with ++itr
. Try:
while (itr != records.end()) {
if (itr->isSelected()) {
itr = records.erase(itr);
recordSize--;
} else {
++itr;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With