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

how to erase a list member with just an iterator (without the list itself)?

I got the following situation: I have a few lists of integers, and I
have an iterator pointing to one of the elements. I don't have a
pointer to the list itself ! All I want is to remove the element the
iterator points to...

Basically, a list element has pointers back and forth, so I shouldn't
need the list itself, but I don't see any function that does the work.
Is there a real problem to do this? Is it just a coincidence that no
body implemented such a function?

Jan 14 '07 #1
6 1683
Should make it clear - talking about STL list..

alon wrote:
I got the following situation: I have a few lists of integers, and I
have an iterator pointing to one of the elements. I don't have a
pointer to the list itself ! All I want is to remove the element the
iterator points to...

Basically, a list element has pointers back and forth, so I shouldn't
need the list itself, but I don't see any function that does the work.
Is there a real problem to do this? Is it just a coincidence that no
body implemented such a function?
Jan 14 '07 #2
Hi,

http://www.sgi.com/tech/stl/List.html
This says, there is a erase function. and it says it does exactly what
you are expecting out of it..

Couldn't get your question really.

~M
alon wrote:
I got the following situation: I have a few lists of integers, and I
have an iterator pointing to one of the elements. I don't have a
pointer to the list itself ! All I want is to remove the element the
iterator points to...

Basically, a list element has pointers back and forth, so I shouldn't
need the list itself, but I don't see any function that does the work.
Is there a real problem to do this? Is it just a coincidence that no
body implemented such a function?
Jan 14 '07 #3
I'll try and clarify my question:

The erase function requires a pointer to the list.
list<intL;
list<int>::iterator iter;
L.erase(iter);

In my case there is no pointer to the list, so what I need is something
like this:
list<int>::erase(iter);

Does this clarify my question?
Milind wrote:
Hi,

http://www.sgi.com/tech/stl/List.html
This says, there is a erase function. and it says it does exactly what
you are expecting out of it..

Couldn't get your question really.

~M
alon wrote:
I got the following situation: I have a few lists of integers, and I
have an iterator pointing to one of the elements. I don't have a
pointer to the list itself ! All I want is to remove the element the
iterator points to...

Basically, a list element has pointers back and forth, so I shouldn't
need the list itself, but I don't see any function that does the work.
Is there a real problem to do this? Is it just a coincidence that no
body implemented such a function?
Jan 14 '07 #4
alon wrote:
I'll try and clarify my question:

The erase function requires a pointer to the list.
No, erase is a member function of the list, and therefore requires a
list object.
list<intL;
list<int>::iterator iter;
L.erase(iter);

In my case there is no pointer to the list, so what I need is something
like this:
list<int>::erase(iter);

Nope. How does erase know what list to erase from? Again, erase is a
*MEMBER FUNCTION* and therefore requires the object.
Jan 14 '07 #5
"alon" <al******@gmail.comwrote:
I got the following situation: I have a few lists of integers, and I
have an iterator pointing to one of the elements. I don't have a
pointer to the list itself ! All I want is to remove the element the
iterator points to...

Basically, a list element has pointers back and forth, so I shouldn't
need the list itself, but I don't see any function that does the work.
Is there a real problem to do this? Is it just a coincidence that no
body implemented such a function?
It cannot be done, and that was by design. Removing an element from a
list without notifying the list is poor practice and makes some list
optimizations difficult to implement. For example, a list may contain a
"size_" member variable in order to speed up the call to myList.size().
Removing a element without telling the list would corrupt that member.

Of course, if each list iterator had a pointer to the list it belonged
too, then it could be done, but the C++ committee apparently decided not
to do that.
Jan 14 '07 #6
"if each list iterator had a pointer to the list " - in practice, this
is what I ended up doing :)

Thx,
Alon
Daniel T. wrote:
"alon" <al******@gmail.comwrote:
I got the following situation: I have a few lists of integers, and I
have an iterator pointing to one of the elements. I don't have a
pointer to the list itself ! All I want is to remove the element the
iterator points to...

Basically, a list element has pointers back and forth, so I shouldn't
need the list itself, but I don't see any function that does the work.
Is there a real problem to do this? Is it just a coincidence that no
body implemented such a function?

It cannot be done, and that was by design. Removing an element from a
list without notifying the list is poor practice and makes some list
optimizations difficult to implement. For example, a list may contain a
"size_" member variable in order to speed up the call to myList.size().
Removing a element without telling the list would corrupt that member.

Of course, if each list iterator had a pointer to the list it belonged
too, then it could be done, but the C++ committee apparently decided not
to do that.
Jan 15 '07 #7

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

Similar topics

2
by: s | last post by:
Here is a snippet of my code: <code> list< MyClass * >outgoing_pool; MyClass* GetWaitingObject(string freq) { MyClass *temp_ptr = NULL; list<MyClass*>::iterator i;
5
by: Angus Leeming | last post by:
Dinkumware's online STL reference http://tinyurl.com/3es52 declares std::map's overloaded erase member functions to have the interface: map::erase iterator erase(iterator where); iterator...
7
by: jose luis fernandez diaz | last post by:
Hi, Is this right any stl container (vector, deque, list, . . .)? typedef vector container; int main() { container<int> c1;
11
by: moleskyca1 | last post by:
Hi, I know if you call erase when you iterate through map you will crash. Ex: map<int,doublem; // insert something for ( map<int, double>::iterator i = m.begin(); i != m.end(); i++ ) if (...
16
by: Frank Neuhaus | last post by:
Hi I have some std list, I'd like to traverse. During the traversal, I want to conditionally delete some objects. My code for that is like this right now: for (std::list<myStruct>::iterator...
6
by: catphive.lists | last post by:
Is there a way to call erase(iter) on a list without invalidating the iterator? Can I make a copy of an iterator and then move forward the original without moving the copy? I'm aware of the...
4
by: sks | last post by:
I have a question regarding std::multimap/iterators. At the SGI website, it says "Erasing an element from a multimap also does not invalidate any iterators, except, of course, for iterators that...
3
by: AbrahamLincolnIllinois | last post by:
Hi all. I have a list of pointers to a complicated object. When I erase() a member of that list, the little blob of memory that contains the pointer is deleted, I think. But the object pointed...
12
by: Philip Mueller | last post by:
Hi, I am using multiple stl::list s of different types. Now I want to write a function list<type>::iterator s_erase(list<typel, list<type>::iterator it) that takes an iterator and deletes...
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: 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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.