Code here: http://codepad.org/zbJCVnL0
Picture of what I mean here:
The while loop that finds where the '(' or ')' values in the list occur and erases them, then outputs (by dereferencing) the element of the list after it has erased the bracket element...but it outputs what shouldn't be there anymore!
Dereferencing the iterator immediately after using iter = object.erase(iter) gives the former value that should have been erased.
But then in the following for loop, the list is displayed in full using dereferencing, and all those elements that should have been erased, are erased and do not appear. What is happening here?
Just follow through the logic of your code carefully by hand and you'll see that this is what it should output.
For example, why does it output a ")"? Well, when you process the "d" before the ")", you see that "d" is not a "(" or ")", increment the iterator, and output what it points to after incrementing it, which is now the ")".
Why does it output the second "("? Well, the rule for ")" says to output the character after it, which in this case is "(".
Your code does exactly what you should have expected. Changing your code to report what it's doing may make it easier to understand:
cout << "starting loop" << endl;
while (sit != llist.end())
{
cout << endl << "The iterator points to '" << *sit << "'." << std::endl;
if (*sit == '(') { cout << "Rule 1"; sit = llist.erase(sit); cout << *sit; }
else if (*sit == ')') { cout << "Rule 2"; sit = llist.erase(sit); cout << '\n' << *sit; }
else { sit++; if (sit != llist.end()) { cout << "Rule 3" ; cout << *sit; } }
}
cout << "loop finished" << endl;