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++;
} 6 1448
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
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.
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.
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
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
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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?
|
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...
|
by: JustSomeGuy |
last post by:
I have a few classes...
class a : std::list<baseclass>
{
int keya;
};
class b : std::list<a>
{
int keyb;
|
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.
...
|
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...
| |
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...
|
by: michael |
last post by:
Hi All,
I have :
#include <iostream>
#include <list>
using std::cout;
using std::list;
using std::endl;
|
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...
|
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?
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |