The Enigma
The Enigma

Reputation: 147

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
list<int> v {1,2,3,4}; 
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
list<int> v{1,2,3,4}; 
auto begin = v.begin(),
end = v.end(); 
while (begin != end) {
    begin+=1; //or alternatively begin = begin +1 
    begin = v.insert(begin, 42);  // insert the new value
    ++begin;  // advance begin past the element we just added
}

Upvotes: 0

Views: 277

Answers (1)

Roman Kolesnikov
Roman Kolesnikov

Reputation: 12147

According to standart std::list implements bidirectional iterator http://www.cplusplus.com/reference/iterator/BidirectionalIterator/ that doesn't have "+=" operator

Upvotes: 1

Related Questions