473,651 Members | 2,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5750
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<typena me Iterator>
Iterator operator+(Itera tor const& input,size_t offset) {
Iterator output = input;
while(offset > 0) {
++output;
offset--;
}
return output;
}

I can't find a forward_iterato r 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<typena me Iterator>
Iterator operator+(Itera tor const& input,size_t offset) {
Iterator output = input;
while(offset > 0) {
++output;
offset--;
}
return output;
}

I can't find a forward_iterato r 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
3660
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 mentioned: I characterized one way of looking at languages in this way: a lot of them are either the agglutination of features or they're a crystallization of style. Languages such as APL, Lisp, and Smalltalk are what you might call style...
1
2830
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 ip: 192.168.0.1 How many ip do you want to connect to: 10 Then, my program will create an array to store 10 ip addresses, starting from 192.168.0.1
2
3324
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
9971
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 algorithm? vector<int> srcVector; vector<int> destVector; for (vector<int>::iterator itr = srcVector; itr != srcVector; itr++) { int i = (*itr);
8
6395
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 a new element, and then deleting the old. typedef multimap<double, MyStructType, greater<double> >;MyMultimap MyMultimap map = ...
2
2005
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
1576
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 (nList<T>::Link). The Link class is defined inside of a template. (I assume that is possible?). Any one know what I doing wrong? Many Thanks, Purush Error from Compiler (gcc 3.4.6) nList.cc:52: error: expected `;' before "current_" *** Error code 1
2
2282
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 itertools.chain(short sequences) or put the short sequences as the first zip arg. To test without consuming, wrap the iterator in a trivial-to-write one_ahead or peek class such as has been posted before.
1
6005
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 from Sr. Please guide. Salim Shaikh India
0
8352
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8802
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...
0
8697
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
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
8579
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...
0
7297
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5612
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
4144
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
1587
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.