473,796 Members | 2,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

incrementing an STL list interator

Hi,

lets say I have a iterator of the type:
list<string>::c onst_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 1176
On Wed, 13 Dec 2006 22:42:32 +0500, "Abubakar" <em**********@y ahoo.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.orgwr ote in message
news:bs******** *************** *********@4ax.c om...
On Wed, 13 Dec 2006 22:42:32 +0500, "Abubakar" <em**********@y ahoo.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
2888
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 eq "old"); push @{$oldsessions}, @line; $i++;
2
420
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 in TextPad's implementation of Regular Expressions, you can do a replacement expression like this: \i For every single non-overlapping match, it places an incrementing number starting with zero. Thus, if you searched for ^ (beginning of line...
6
6663
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 give me this capability. So I want std::list<T*>::iterator. However, the container is of type std::list<T*>. How to get std::list<const T*>::iterator?
2
1367
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 and want to know how often I have to call ++it before I arrive at the end of the list. Means how many elements are left after the given iterator it. My first solution (see below) is quite inefficent. int restsize()
4
6552
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 - and insert an entry in that place some time later. There's an "insert" member function, but it takes an iterator to designate the place to insert. And I can't figure out how to get an iterator that points to the right place. For instance: ...
56
5208
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 checked on each iteration, since the Count changes as you delete elements. I would think it is guaranteed to be computed each time, and not cached. So, is this the best way?
5
2387
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
1273
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
3377
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 array of int */\ ap = &a1; printf("%d\n", **ap);
0
9533
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10461
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10190
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10019
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.