473,692 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple Iterator question

Hi,

I am using an iterator on a list, I wanted to know is there a simple
way to erase() the current element the iterator is pointing to and move
the iterator forward one?

The only way I can think of doing this is making a temp iterator equal
to orig position, then erasing, and setting the original iterator to
temp iterator++

Basically asking if there is a way to do the code below in two steps
instead of three.

Thanks,

RishiD

// move private iterator forward and eliminate person
void movingForward()
{
list<Person>::i terator tempIter = privIter;
circle.erase(pr ivIter);
privIter = tempIter++;
}

Oct 30 '06 #1
6 1451
RishiD wrote:
I am using an iterator on a list, I wanted to know is there a simple
way to erase() the current element the iterator is pointing to and
move the iterator forward one?
[..]
Is this a trick question? 'std::list::era se' has a return value
type, use it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 30 '06 #2

Victor Bazarov wrote:
RishiD wrote:
I am using an iterator on a list, I wanted to know is there a simple
way to erase() the current element the iterator is pointing to and
move the iterator forward one?
[..]

Is this a trick question? 'std::list::era se' has a return value
type, use it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Haha sorry. My professor told me otherwise, thanks.

Sorry for wasting your time.

Oct 30 '06 #3
In article <11************ **********@b28g 2000cwb.googleg roups.com>,
"RishiD" <ri****@gmail.c omwrote:
Hi,

I am using an iterator on a list, I wanted to know is there a simple
way to erase() the current element the iterator is pointing to and move
the iterator forward one?
Yes, simply call erase() and assign the return value to the iterator
object passed in.
The only way I can think of doing this is making a temp iterator equal
to orig position, then erasing, and setting the original iterator to
temp iterator++

Basically asking if there is a way to do the code below in two steps
instead of three.

Thanks,

RishiD

// move private iterator forward and eliminate person
void movingForward()
{
list<Person>::i terator tempIter = privIter;
circle.erase(pr ivIter);
privIter = tempIter++;
}
void movingForward()
{
privIter = circle.erase( privIter );
}

or since you are working with a list, you could:

void movingForward()
{
circle.erase( privIter++ );
}

--
To send me email, put "sheltie" in the subject.
Oct 30 '06 #4
Hi

Why not

circle.erase(pr ivIter++);

privIter++ increments the iterator and returns the previous value
voila....

It would work even if the container didnt return an iterator from
erase()
Regards
Vivek

Oct 30 '06 #5
rep_movsd wrote:
Hi

Why not

circle.erase(pr ivIter++);

privIter++ increments the iterator and returns the previous value
voila....

It would work even if the container didnt return an iterator from
erase()
By container, you mean std::list: the idiom you propose does not work for
std::vector because of iterator invalidation. Using the return from erase()
provides the more robust code -- you could change the container to another
sequence type.
Best

Kai-Uwe Bux
Oct 30 '06 #6
"rep_movsd" <re*******@gmai l.comwrote:
Why not

circle.erase(pr ivIter++);
It's not as general purpose because it doesn't work for vector and may
not work for deque depending on where in the container the iterator is.

When you have a choice between a method that works for all Sequences and
one that only works for some Sequences, always choose the more general
approach unless you information that the specific method is faster.
privIter++ increments the iterator and returns the previous value
voila....

It would work even if the container didnt return an iterator from
erase()
All Sequences must return an iterator from erase, they don't all have to
keep the iterator valid.

--
To send me email, put "sheltie" in the subject.
Oct 30 '06 #7

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

Similar topics

2
1846
by: Neal D. Becker | last post by:
x = for i in x: i = 2 This doesn't change x. 2 questions: 1) Why not? Why doesn't assign to an iterator of a mutable type change the underlying object?
4
1884
by: matthurne | last post by:
I am working through exercise 8-2 in Accelerated C++...I am implementing the function equal(b, e, b2) where b is an iterator for the first element in a container, e is an iterator pointing to one past the last element in that same container, and b2 is an iterator for the first element in the second container. Here's what I have: template <class T> bool equal(T begin, T end, T begin2) { while (begin != end) { if (*begin != *begin2) {
10
1850
by: JustSomeGuy | last post by:
I have a few classes... class a : std::list<baseclass> { int keya; }; class b : std::list<a> { int keyb;
2
1635
by: solartimba | last post by:
Hi, I am in another country, and I need to finish a program. But I do not have any reference books. Can you tell me how to print this to a file without ending the stream with an endl. Stated differently, I don't want an endl after the last data point printed to the file. I know this is a basic question, and I apologize. ******************************
1
1295
by: Steve H | last post by:
From "C++ Programming Language (3rd Edition)" I understand that there are 5 standard iterator categories defined in the "std" namespace: Output *p= p++ Input =*p -> ++ == != Forward =*p -> *p= ++ == != Bidirectional =*p -> *p= ++ -- == != Random Access =*p -> *p= ++ -- == != + - += -= < > <= >= I have an iterator-like class for which I would like to ensure
5
2618
by: Mark Stijnman | last post by:
I have a question about forward iterators and what one should do or not do with them. I'm planning on writing a container that, when all boils down to it, stores a sequence of strings. I want threads to be able to read this sequence from start to end, and when they reach the end, wait until new data is added to the end. If so, it should pick up reading where it left off. The question is, is it valid and moral to do something like this from...
4
1459
by: michael | last post by:
Hi All, I have : #include <iostream> #include <list> using std::cout; using std::list; using std::endl;
176
8352
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write their own unit test framework, and then in a lab session see if I can get them to write their own. To give them an example I've created the following UTF class (with a simple test program following). I would welcome and suggestions on how anybody...
16
6072
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last element in the container? Or is there a cleaner way of getting an iterator to the last element?
0
8604
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
9083
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
8961
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...
0
7627
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
5819
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
4324
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...
0
4557
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1957
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.