473,394 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

STL iterator arithmetic

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

Nov 22 '05 #1
3 7051
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)
Nov 22 '05 #2
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
Nov 22 '05 #3
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);
}

Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
12
by: Roel | last post by:
Hello all, The following will not compile under gcc 3.2.2: #include <string> #include <vector> #include <iostream> #include <ctype.h> class A
9
by: Alexander Stippler | last post by:
Hi, I've got trouble with some well known issue. Iterator invalidation. My situation: for (it=v.begin(); it!=v.end(); ++it) { f(*it); } Under some circumstances, f may alter the container...
29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
13
by: Mike Austin | last post by:
Hi all. Just working on a small virtual machine, and thought about using vector iterators instead of pointer arithmetic. Question is, why does an iterator plus any number out of range not...
13
by: Grahamo | last post by:
Hi, I'm implementing a custom iterator (random access type ) so I can use stl algorithms such as sort on my legacy containers. I'm having problems compiling however. when implementing my...
13
by: jois.de.vivre | last post by:
Hi All, I'm trying to write a wrapper class for std::vector to extend some of its functionality. The problem I'm getting into is returning an iterator type from a member function. Here is the...
2
by: ul | last post by:
Hello, Just wonder what is faster for std::vector, const_iterator od iterator? Or is this stl-realisation-dependent? Target compilator gcc platform is Linux. Thanks
12
by: Howard | last post by:
Is there an easy way to get an iterator (*not* a reverse-iterator) to the last element in a list? The last() function returns the element itself, not an iterator. Thanks, -Howard
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.