Q:

Vector iterator not dereferenceable when changing iterator comparisson

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

A:

You should test for the iterator to be a valid iterator before dereferencing it. Your loop will continue until *it < investigated_stat but it won't stop when none of the elements are >= investigated_stat and you iterate past the end of the vector.
The test in the for loop should be
it != stat1vec.end() && *it >= investigated_stat

Which makes the slightly modified for:
for(it = stat1vec.begin(); it != stat1vec.end() && *it >= investigated_stat; ++it, ++positioner)
    if(*it == investigated_stat)
        ++equalwith;

