samedi 1 août 2015

C++ list iterator arithmetic

I am aware that you cannot use iterators with list in the form "it +n" but why is that when I use ++it the program is able to compile i.e:

//program compiles
auto begin = v.begin(),
end = v.end(); 
while (begin != end) {
    ++begin;  
    begin = v.insert(begin, 42); 
    ++begin;  // advance begin past the element we just added
}

//program doesn't compile
auto begin = v.begin(),
end = v.end(); 
while (begin != end) {
    begin+=1;  
    begin = v.insert(begin, 42);  // insert the new value
    ++begin;  // advance begin past the element we just added
}

Aucun commentaire:

Enregistrer un commentaire