I have a list list<x> bar
and an array of iterators list<x>::iterator foo[100]
initialized all elements with bar.end()
.
I want to store pointers in foo
to some elements in bar
while I'm going to erase, insert and push_back elements into bar
. For element insertion I have to check if a position in foo
is empty or not:
if (foo[my_number] != bar.end())
//do something 1
else
//do something 2
I receive an error only in case the foo[my_number]
is already pointing to a bar
element. The if
is true I got this error (if false, no problem):
Debug Assertion Failed!
Program: C:\Windows\system32\MSVCP120D.dll
File: C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\list
Line: 289
Expression: list iterators incompatible
I read here that after insertion the bar.end()
is not the same as before. However, my code doesn't fail if I push back elements and compare a bar.end
and foo[k] = end
. But fails to compare bar.end
to foo[k] = bar.begin() + p
(this bar
element exist).
What am I missing? How can I overcome this problem? Should I re-actualize foo
after I inserted elements?