![]() |
Re: std::vector iterator arithmetic
Michael D. Berger wrote:
> Is std::vector arithmetic allowed? For example: > > std::vector<unsigned> vec; > > ... // put things in vec . > > vec.erase(vec.begin(),vec.begin()+2); > > erases first two item in vec . OK? > > Chapter & verse? > Yes, the iterators are "random access iterators" which allows adding or subtracting from them. Other containers have other kinds of iterators, and the compiler will tell you when addition doesn't work. One example is std::list, whose iterators can be incremented and decremented, but not added to. Bo Persson |
Re: std::vector iterator arithmetic
Bo Persson wrote:
> Yes, the iterators are "random access iterators" which allows adding > or subtracting from them. > > Other containers have other kinds of iterators, and the compiler will > tell you when addition doesn't work. One example is std::list, whose > iterators can be incremented and decremented, but not added to. Which has the side effect that the O(1) operation iter+1 looks rather funny because you have to invoke the copy constructor explicitely. Marcel |
| All times are GMT. The time now is 03:59 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.