I have a code:
it = tableAndHand.begin();
while(++it != tableAndHand.end()) {
if(*it == *(--it)) {
++cardCount;
++it;
} else {
cardCounts1.insert(pair<int,int>(cardCount,*it));
while(cardCount > 1) {
it = tableAndHand.erase(--it);
--cardCount;
}
++it;
}
}
cardCounts1.insert(pair<int,int>(cardCount,*(--it)));
while(cardCount > 1) {
it = tableAndHand.erase(--it);
--cardCount;
}
tableAndHand is a list of 7 values at start and I get the Segmentation fault in that problematic place after erasing some values, why is it so?
The values in list are sorted and it fails on list {0, 0, 0, 1, 1, 1, 2} somewhere iterating the 1s (after correctly erased 2 0s, so the list's size is already 5 ).
I just want to save the count of unique values into map cardCounts1 and erase the repeated values from list, what is wrong on my algorithm?
EDIT: Looks like the problem was (*it == *(--it)) is not evaluated from left to right although I can't find the evaluation of "==" on articles about operators on cplusplus.com and on some other sites they say it's evaluated from left to right. Some good link about it?
EDIT2: OK, it works, I forgot to assign the tableAndHand.erase(--it) iterator to it, now it works perfectly and fast :)