danny
danny

Reputation: 135

Runtime error when using vector iterator

I'm having a problem with the following code:

for(int j = 0; j < ensembleTemp.size(); j++)
        {
            ensemble[ensembleTemp[j]].clear();
            ensemble[ensembleTemp[j]].insert(ensemble[j].begin(),
                                     ensembleTemp.begin(), ensembleTemp.end());
        }   

ensembleTemp is a vector<int> and ensemble is a vector<vector<int>>. I have the following,

error: vector insert iterator outside range.

What's my mistake?

Upvotes: 0

Views: 766

Answers (2)

Adam Bowen
Adam Bowen

Reputation: 11240

You're using the wrong index for the first parameter of insert, it (presumably) should be

for(int j = 0; j < ensembleTemp.size(); j++)
{
    ensemble[ensembleTemp[j]].clear();
    ensemble[ensembleTemp[j]].insert(
        ensemble[ensembleTemp[j]].begin(), 
        ensembleTemp.begin(), 
        ensembleTemp.end());
}

The first parameter to insert should be an iterator for the vector being inserted into.

In addition ensemble.size() must be greater than ensembleTemp[j] for all j.

Upvotes: 3

Miguel Angel
Miguel Angel

Reputation: 640

Are you sure ensemble.size() is greater than 'j'? and greater than ensembleTemp[j]?

Upvotes: 0

Related Questions