Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Re: std::vector iterator arithmetic (http://www.velocityreviews.com/forums/t701222-re-std-vector-iterator-arithmetic.html)

Bo Persson 10-11-2009 07:04 PM

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



Marcel Müller 10-11-2009 07:37 PM

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 06:53 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.