STL iterator arithmetic

  • Thread starter Generic Usenet Account
  • Start date
G

Generic Usenet Account

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
 
P

Pete Becker

Generic said:
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.
 
V

Victor Bazarov

Generic said:
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
 
G

Generic Usenet Account

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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top