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

next iterator position without mutation

Greetings.

Is there a convinient way to get the next iterator position without mutating
the iterator for non-random access containers? For example,

i + 1

instead of

i++

Thanks
Jul 19 '05 #1
5 5736
Cagdas Ozgenc wrote:
Greetings.

Is there a convinient way to get the next iterator position without
mutating the iterator for non-random access containers? For example,

i + 1

instead of

i++


Nope. For input or output iterators you cannot even read the same place
twice. I might be wrong, but for forward or bidirectional iterators you may
copy the iterator and increment the copy to "take a peek".

--
Attila aka WW
Jul 19 '05 #2
> Nope. For input or output iterators you cannot even read the same place
twice. I might be wrong, but for forward or bidirectional iterators you may copy the iterator and increment the copy to "take a peek".

That's bad news. The following code would be quite dangerous then:

template<typename Iterator>
Iterator operator+(Iterator const& input,size_t offset) {
Iterator output = input;
while(offset > 0) {
++output;
offset--;
}
return output;
}

I can't find a forward_iterator class to make the above function iterator
type-safe. What's wrong with these STL iterators?

Thanks
Jul 19 '05 #3
Cagdas Ozgenc wrote:
Nope. For input or output iterators you cannot even read the same
place twice. I might be wrong, but for forward or bidirectional
iterators you may copy the iterator and increment the copy to "take
a peek".

That's bad news. The following code would be quite dangerous then:

template<typename Iterator>
Iterator operator+(Iterator const& input,size_t offset) {
Iterator output = input;
while(offset > 0) {
++output;
offset--;
}
return output;
}

I can't find a forward_iterator class to make the above function
iterator type-safe. What's wrong with these STL iterators?


You have iterator traits, OMHO you can make a compile time assertion if the
function template is being instantiated with the wrong iterator category.

--
Attila aka WW
Jul 19 '05 #4
> You have iterator traits, OMHO you can make a compile time assertion if
the
function template is being instantiated with the wrong iterator category.


Can you give a small example, or give pointers on that matter.

Thanks
Jul 19 '05 #5
Cagdas Ozgenc wrote:
You have iterator traits, OMHO you can make a compile time assertion
if the function template is being instantiated with the wrong
iterator category.


Can you give a small example, or give pointers on that matter.


10 seconds using Google:
http://www.roguewave.com/support/doc...ator-traits.ht
ml

--
Attila aka WW
Jul 19 '05 #6

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...
1
by: Abby | last post by:
I'm writing a code to send/receive udp packet. I'll receive ip address from user (this is the starting ip), then I'll ask user how many ip he wants to connect to. For examples: Please enter 1st...
2
by: systemspecialist | last post by:
Hi, I need to detect mouse position without events. In some places in code I need to get position but mouse didn't do anything(click,move...). Thanks
7
by: Allerdyce.John | last post by:
How can I emove an item in STL iterator without use the STL algorithm? I know we can use stl erase, find_if to remove items from a STL vector , but how can I do the same without using STL...
8
by: Steve Edwards | last post by:
Hi, While iterating through a multimap, I need to replace elements that meet certain conditions with a new element. There doesm't seem to be a replace() function for multimaps, so I'm inserting...
2
by: srini4vasan | last post by:
#include <stdio.h> int main() { char n, m; puts (" Enter the first string and . to terminate :"); do { n = getchar(); putchar(n);
3
by: pnayak | last post by:
Hi, I am trying to implement an iterator to a List class. The code is included below. I get the following compiler error. At the point where I use a class that is defined inside a template...
2
by: Terry Reedy | last post by:
Luis Zarrabeitia wrote: Interesting observation. Iterators are intended for 'iterate through once and discard' usages. To zip a long sequence with several short sequences, either use...
1
by: Salim Shaikh | last post by:
Joinning asJunior Network engineer with out any knowledge in the same field, how i start 1st day in company ,should folloup with Sr. Engineer to grab knowledge or any other process i need to take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.