[Problem] Segmentation fault when using list iterator
Dear everybody,
I got a segmentation fault but don't know what went wrong. I searched this forum but couldn't find any help.
Here is the code
I have a list
|
std::list<CRegion> manager;
|
then, I would like to traverse backward the whole list
// at this time, I have 1 element in my list (size =1)
1 2 3 4
|
list<CRegion>::iterator it;
for (it = manager.end(); it != manager.begin(); it--) {
CRegion reg = *it; //segmentation fault here
}
|
I just tried to get an element of the list but it failed. Are there other ways where we can get list's elements yet using iterator?
Thank you so much for your time and consideration.
end() is never a valid iterator. If you want to traverse backwards you should start with rbegin() while !=rend().
Thank you so much for your responsive and helpful answer, helios. I didn't know that we can use reverse_iterator together with rbegin and rend.
Topic archived. No new replies allowed.