473,396 Members | 1,766 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,396 software developers and data experts.

incrementing an STL list interator

Hi,

lets say I have a iterator of the type:
list<string>::const_iterator itr;
lets say its at some position in a given list. Now I can goto the next item
by simply doing a itr++. But I want to increment more than just 1. Lets says
I want to increment the itr 3 times, ie being able to do something like
itr+=3. But as the += is not defined for that iterator class, I cant do
that. What should I do?

Regards,

-ab.
Dec 13 '06 #1
2 1155
On Wed, 13 Dec 2006 22:42:32 +0500, "Abubakar" <em**********@yahoo.com>
wrote:
>Hi,

lets say I have a iterator of the type:
list<string>::const_iterator itr;
lets say its at some position in a given list. Now I can goto the next item
by simply doing a itr++. But I want to increment more than just 1. Lets says
I want to increment the itr 3 times, ie being able to do something like
itr+=3. But as the += is not defined for that iterator class, I cant do
that. What should I do?
Look into std::advance. It can handle any iterator type and will repeatedly
increment non-random access iterators as necessary. The operator isn't
provided for non-random access iterators to emphasize that increment values
1 aren't as simple and efficient as pointer arithmetic.
--
Doug Harrison
Visual C++ MVP
Dec 13 '06 #2
Wow, pretty cool. Thanks :)

-ab.

"Doug Harrison [MVP]" <ds*@mvps.orgwrote in message
news:bs********************************@4ax.com...
On Wed, 13 Dec 2006 22:42:32 +0500, "Abubakar" <em**********@yahoo.com>
wrote:
>>Hi,

lets say I have a iterator of the type:
list<string>::const_iterator itr;
lets say its at some position in a given list. Now I can goto the next
item
by simply doing a itr++. But I want to increment more than just 1. Lets
says
I want to increment the itr 3 times, ie being able to do something like
itr+=3. But as the += is not defined for that iterator class, I cant do
that. What should I do?

Look into std::advance. It can handle any iterator type and will
repeatedly
increment non-random access iterators as necessary. The operator isn't
provided for non-random access iterators to emphasize that increment
values
>1 aren't as simple and efficient as pointer arithmetic.

--
Doug Harrison
Visual C++ MVP

Dec 13 '06 #3

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

Similar topics

3
by: Mothra | last post by:
Here's what I'm trying to do (kill off old Unix logins): --------------------- $i=0; while (<$who>) { chomp($_); my @line = split(/\s+/, $_); # Split it into an array next unless ($line...
2
by: brian | last post by:
Hi, before coming to .NET, I utilized regular expressions mostly in JScript / JavaScript and also in my favorite text editor: TextPad (www.textpad.com) I don't know about JScript/JavaScript, but...
6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
2
by: Thomas Kowalski | last post by:
Hi everyone, I would like if there is an easy way to get the number of elements left after an given iterator? I have an interator called it somethere between beginning and end of an list / map...
4
by: Tim Slattery | last post by:
It would be convenient for my app to store the stuff I'm generating in a std::list. I'd like to remember the location of a particular place in the list - sort of like sticking my finger into it -...
56
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be...
5
by: Renier | last post by:
Hi, I'm new to this interator thing. Can I check if a interator != NULL. It gives an compiling error on VC8, but not VC6. Please assist.
3
by: =?Utf-8?B?TWlrZSBL?= | last post by:
I have a Net 2.0 app in VB that has code like this: Dim intA as Integer Dim intB as Integer dim myList as new List(Of String) intA = 10 intB = 20 .... code to fill the list ....
9
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
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
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.