473,396 Members | 2,129 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.

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>::iterator tempIter = privIter;
circle.erase(privIter);
privIter = tempIter++;
}

Oct 30 '06 #1
6 1437
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::erase' 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::erase' 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**********************@b28g2000cwb.googlegroups .com>,
"RishiD" <ri****@gmail.comwrote:
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>::iterator tempIter = privIter;
circle.erase(privIter);
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(privIter++);

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(privIter++);

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*******@gmail.comwrote:
Why not

circle.erase(privIter++);
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
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
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...
10
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
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...
1
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 -> ++ == !=...
5
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...
4
by: michael | last post by:
Hi All, I have : #include <iostream> #include <list> using std::cout; using std::list; using std::endl;
176
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...
16
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.