简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM