Errors: division by zero and iterator not dereferencable

-1

I'm having trouble compiling this code, it returns me two errors when I run it:

vector<bool> cp( size );

// determine crossover point (randomly)
for( int i = _numberOfCrossoverPoints; i > 0; i-- )
{
    while( 1 )
    {
        int p = rand() % size;
        if( !cp[ p ] )
        {
            cp[ p ] = true;
            break;
        }
    }
}

The first error is related to the definition of p, it says that divide by zero occurs.

The second error is related to the attempt to access cp with such p, it says iterator not dereferencable.

I wanted to understand why these errors are returned and how to solve them.

    
asked by anonymous 07.11.2018 / 03:33

1 answer

0

The divide-by-zero error in int p = rand() % size; will occur if the variable "size" is zero. The second error is probably a consequence of the first error.

    
07.11.2018 / 04:03