Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL iterator arithmetic

Reply
Thread Tools

STL iterator arithmetic

 
 
Generic Usenet Account
Guest
Posts: n/a
 
      11-15-2005
This posting is just for clarification of my understanding. It appears
to me that only vector and deque iterators (i.e. random access
iterators) allow "iterator arithmetic" operations (like iter+2, iter-1
etc.). Kindly confirm.

Thanks,
Song

 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      11-15-2005
Generic Usenet Account wrote:
> This posting is just for clarification of my understanding. It appears
> to me that only vector and deque iterators (i.e. random access
> iterators) allow "iterator arithmetic" operations (like iter+2, iter-1
> etc.). Kindly confirm.
>


Forward iterators support incrementing.
Bidirectional iterators support incrementing and decrementing.
Random access iterators support incrementing, decrementing, and the sort
of arithmetic that you've mentioned.

In the containers provided by the Standard C++ Library, the sequences
managed by vector and deque have random access iterators. The sequences
managed by list, set, multiset, map, and multimap have bidirectional
iterators.

In containers from other sources, check the documentation.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-15-2005
Generic Usenet Account wrote:
> This posting is just for clarification of my understanding. It appears
> to me that only vector and deque iterators (i.e. random access
> iterators) allow "iterator arithmetic" operations (like iter+2, iter-1
> etc.). Kindly confirm.


Also, basic_string iterators are of random access variety.

V
 
Reply With Quote
 
Generic Usenet Account
Guest
Posts: n/a
 
      11-16-2005
I am not seeing the method definition for deleting elements at a
specified position in a vector or deque. Is that the way it is
supposed to be? Here's what I have come up with. Is there a better
way around?

Thanks,
Song

////////////////////////////////////////////
template<typename T>
void
eraseAtPosition(vector<T>& coll, size_t posn, size_t numElem = 1)
{
if(posn+numElem > coll.size())
{
cerr << "Out of bounds exception\n";
return;
}

coll.erase(coll.begin()+posn, coll.begin()+posn+numElem);
}

 
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
Re: std::vector iterator arithmetic Bo Persson C++ 1 10-11-2009 07:37 PM
std::map::iterator arithmetic problems Christopher C++ 0 01-13-2009 10:11 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