samedi 4 novembre 2017

Vector iterator + offset out of range error thrown incorrectly

I have written code for an evolving neural network and one of my classes has the following member function:

bool Brain::makeLayer(int pos)
{
    int preSize = layers.size();

    layers.insert(layers.begin() + pos, std::vector<Cell*>{});

    return layers.size() > preSize;
 }

where layers is defined as std::vector<std::vector<Cell*>> layers

I am getting an error vector + offset out of range on the line layers.insert(layers.begin() + pos, std::vector<Cell*>{});

Through some debugging, the size of layers is 1 and pos is also set as 1.

I have tried testing with different values of pos, and if called directly makeLayer(1) works as intended...

How it should work, layers.begin() + pos should be equal to layers.end() for a vector of size 1, and so when insert is called at that position, it should simply insert a std::vector<Cell*>{} at the end of layers, but it does not and throws the aforementioned error instead.

Aucun commentaire:

Enregistrer un commentaire