The following code snippet gives the error message at run time of:
Debug assertion failed: Vector iterator not dereferenceable
for(it=stat1vec.begin(); *(it)>=investigated_stat; it++, positioner++)
{
if(*it==investigated_stat)
equalwith++;
}
When changed to:
for(it=stat1vec.begin(); *(it)==investigated_stat; it++, positioner++)
{
if(*it==investigated_stat)
equalwith++;
}
The code works perfectly.
All the has been done is to change >= to ==
Why is this?
Thanks very much
You should test for the iterator to be a valid iterator before dereferencing it. Your loop will continue until
*it < investigated_statbut it won’t stop when none of the elements are>= investigated_statand you iterate past the end of the vector.The test in the for loop should be
Which makes the slightly modified
for: