ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
When I take out the erase the program works, but it does nothing, is there a bug in the stl list... or am I just doing something amazingly stupid that I fail to see, or did I find a bad site or what?
Or else your loop will try to go through all 20 items of the list, the only problem is there are only 19 now, and the additional int_iter++ for arg 2 of the erase will make it go for 21.
ok, the problem is with the erase as you assummed, you need to remember that end is one after the last element so erase(a,b) erases the range [a,b) that is from a to b excluding b
now you are calling erase(int_iter,(int_iter++)) which i reckon compiles to something like
blah, I can't believe I left that in, if you take out the ++, it still doesn't work, that was me messing around with it and trying to figure out what's wrong
Did you try using int_iter-- ? This mainly prevents another seg fault to be incase you delete the last item in the list... it also works for what you are doing:
int_iter-- only works because list<> is doubally(sp?) linked, this wont work for a singly linked list. ie slist<>
the code i gave above "erase(int_iter++)" will work for both a singly and a doubally linked list. also there are no problems at the end because if you delete the last item int_iter++ will equal int_list.end() causing you to drop out of the loop before you try and dereference.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.