简体   繁体   中英

List Iterator not dereferencable

I have been looking for the same problem I have with iterators; and I found a lot of topics with almost the same title, and similar problems, but mine is a little bit different.

I'm trying to get the *iterator at a specific position, but I get the error: "list iterator not deferencable" at run time, here is my code:

 CompetidorFormula2000* retornarCompetidorF2000(int pos){
        list<Competidor*>::iterator itr=miLista.begin();
        CompetidorFormula2000* f1=new CompetidorFormula2000(); 

        if(pos>0 && pos<=miLista.size()){
            advance(itr,pos);

        }
        f1=(CompetidorFormula2000*) (*itr);
        return f1;
        delete f1;
    }

If pos equals miLista.size() then you will get an iterator to the end of the list, the same iterator you get when you call miLista.end() . An iterator to the end of the list is not dereferenceable .

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