Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: std::vector iterator arithmetic

Reply
Thread Tools

Re: std::vector iterator arithmetic

 
 
Bo Persson
Guest
Posts: n/a
 
      10-11-2009
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


 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      10-11-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::set::iterator cannot perform arithmetic? Christopher C++ 9 11-06-2012 11:03 PM
std::map::iterator arithmetic problems Christopher C++ 0 01-13-2009 10:11 PM
STL iterator arithmetic Generic Usenet Account C++ 3 11-16-2005 04:42 PM
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM
Iterator/pointer arithmetic Marc Schellens C++ 15 12-08-2003 01:32 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57