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

list remove and insert

Hi
If've just read, that remove() on a list does not actually remove the
elements, but places them at the end of the list (according to TC++STL
by Josuttis). It also says, that remove returns a new, logical end
pointer, so that the following

myList::iterator end = myListObj.remove(myInt);
myListObj.erase(end, myListObj.end());

is possible and removes the "invalid" items at the end of the list.
Now my question - is the following statement still vaild?

myList::iterator end = myListObj.remove(myInt);
myListObj.insert(anotherInt);
myListObj.erase(end, myListObj.end());

or does the iterator "end" not change?
In my actual code I do some deleting and inserting after reusing the
list (iterating over all elements). So I would like to do some kind of
"lazy" correcting, which means doing the "erase-step" just before I
start iterating over all elements again.

Thanks
Arne

Oct 4 '05 #1
6 4921
On Tue, 4 Oct 2005 14:19:06 +0200, Arne Claus <ar*****@uni-koblenz.de>
wrote:
Hi
If've just read, that remove() on a list does not actually remove the
elements, but places them at the end of the list (according to TC++STL
by Josuttis). It also says, that remove returns a new, logical end
pointer, so that the following

myList::iterator end = myListObj.remove(myInt);
myListObj.erase(end, myListObj.end());

is possible and removes the "invalid" items at the end of the list.
Now my question - is the following statement still vaild?

myList::iterator end = myListObj.remove(myInt);
myListObj.insert(anotherInt);
myListObj.erase(end, myListObj.end());

or does the iterator "end" not change?
In my actual code I do some deleting and inserting after reusing the
list (iterating over all elements). So I would like to do some kind of
"lazy" correcting, which means doing the "erase-step" just before I
start iterating over all elements again.

Thanks
Arne


I don't believe that will work:

- std::list<>::remove() returns void;
- std::list<>::insert() has three overloads, all of which take an
iterator as the first argument.

I think you need to look at the way std::list works some more.

--
Bob Hairgrove
No**********@Home.com
Oct 4 '05 #2
Arne Claus wrote:
Hi
If've just read, that remove() on a list does not actually remove the
elements, but places them at the end of the list (according to TC++STL
by Josuttis). It also says, that remove returns a new, logical end
pointer, so that the following

myList::iterator end = myListObj.remove(myInt);
myListObj.erase(end, myListObj.end());

is possible and removes the "invalid" items at the end of the list.
Now my question - is the following statement still vaild?

myList::iterator end = myListObj.remove(myInt);
myListObj.insert(anotherInt);
std::list::insert needs an iterator that tells it where to insert the new
value.
myListObj.erase(end, myListObj.end());

or does the iterator "end" not change?


Your code is valid. What it does depends on where you inserted the new
value. If it was inserted before "end", it will stay in the list.
Otherwise, it will be erased.

Oct 4 '05 #3
Arne Claus wrote:
Hi
If've just read, that remove() on a list does not actually remove the
elements, but places them at the end of the list (according to TC++STL
by Josuttis).
I think you're referring to std::remove. The remove member function of
the list class really does remove elements, and it doesn't return
anything.
It also says, that remove returns a new, logical end pointer, so that
the following

myList::iterator end = myListObj.remove(myInt);
What the heck is a myList? If you've rolled your own list class, it
doesn't matter what the Josuttis book says about lists. I'm going to
give you the benefit of the doubt and assume you really want to use
std::list. Next time, please post real, compilable code.
myListObj.erase(end, myListObj.end());
This is commonly called the Erase-Remove Idiom, but it uses the remove
function from the header <algorithm>.
is possible and removes the "invalid" items at the end of the list.
Now my question - is the following statement still vaild?

myList::iterator end = myListObj.remove(myInt);
Again, if you want that behavior, you need std::remove.
myListObj.insert(anotherInt);
This is an error. All of the insert functions in std::list take at
least two arguments. I suspect you're looking for a function like
push_front.
myListObj.erase(end, myListObj.end());

or does the iterator "end" not change?
If you coded the preceeding lines properly, that line would be valid.
insert, remove, and erase are all guaranteed not to invalidate
iterators for a list.
In my actual code I do some deleting and inserting after reusing the
list (iterating over all elements). So I would like to do some kind of
"lazy" correcting, which means doing the "erase-step" just before I
start iterating over all elements again.


But you've already looped over all the elements with the remove
function, regardless of which one you use. Why not just use
list::remove to search and erase all in one step?

Kristo

Oct 4 '05 #4
"Arne Claus" <ar*****@uni-koblenz.de> wrote in message
news:dh**********@cache.uni-koblenz.de...
If've just read, that remove() on a list does not actually remove the
elements, but places them at the end of the list (according to TC++STL by
Josuttis). It also says, that remove returns a new, logical end pointer,
so that the following myList::iterator end = myListObj.remove(myInt);
myListObj.erase(end, myListObj.end()); is possible and removes the "invalid" items at the end of the list.


I think that perhaps you may have misunderstood. The behavior you describe
applies to the remove algorithm (well, not quite, but close), not to the
remove member of the list class.
Oct 4 '05 #5
Ok - there were some mistakes on my side (the code wasn't meant to be
100% correct c++ - my bad :)
I think you're referring to std::remove. The remove member function of
the list class really does remove elements, and it doesn't return
anything.


Ah - that's nice to know for a start. Josuttis says something about
using the list-member functions because those are faster in terms of
element access. If they do really remove the element (and thus
producing a correct list) this is fine (but also a bit slower I guess).
It also says, that remove returns a new, logical end pointer, so that
the following

myList::iterator end = myListObj.remove(myInt);


What the heck is a myList? If you've rolled your own list class,


typedef list<int> myList;

I wanted to abbrief the template a bit here.
myListObj.insert(anotherInt);


This is an error. All of the insert functions in std::list take at
least two arguments. I suspect you're looking for a function like
push_front.


Ok - that's my mistake now. I normally use push_back in my code and not insert.
As I said - it was originally meant to be c++ alike pseudo code (and I
should have said so in the first place - sorry).
myListObj.erase(end, myListObj.end());

or does the iterator "end" not change?


If you coded the preceeding lines properly, that line would be valid.
insert, remove, and erase are all guaranteed not to invalidate
iterators for a list.


Which means that end (retrieved by std::remove) would *not* change,
even with a push_back - if I understand that correct.
In my actual code I do some deleting and inserting after reusing the
list (iterating over all elements). So I would like to do some kind of
"lazy" correcting, which means doing the "erase-step" just before I
start iterating over all elements again.


But you've already looped over all the elements with the remove
function, regardless of which one you use. Why not just use
list::remove to search and erase all in one step?


Well - If I've got a list of - let's say 500k Objects, and I want to
remove 250k of them (random values) - what's faster std::remove with
erase afterwards or list::remove?
My guess whould be the first variant because here erase handles many
items in a row, whereas in the second case the items are scattered
around.

I thought about a vector beeing a faster alternative here, too (random
remove + adding at the end), but I'm not quite sure about this. Any
suggestions would be helpfull there, too ^^

Thank you
Arne

Oct 4 '05 #6
Arne Claus wrote:
Well - If I've got a list of - let's say 500k Objects, and I want to
remove 250k of them (random values) - what's faster std::remove with
erase afterwards or list::remove?
My guess whould be the first variant because here erase handles many
items in a row, whereas in the second case the items are scattered
around.
list::remove is faster in general, or else the standards people
wouldn't have provided it as a member function. To delete an element
from a list, all you have to do is reassign a couple of pointers. You
need the erase-remove idiom for the cases (i.e., most of the time) when
deleting elements is not so easy. std::remove also involves a lot of
copying, whereas list::remove does not.

In either case, you have to examine every element. It doesn't matter
where they are in the list.
I thought about a vector beeing a faster alternative here, too (random
remove + adding at the end), but I'm not quite sure about this. Any
suggestions would be helpfull there, too ^^


std::vector provides random *access*, not random removal of elements.
You'd need the erase-remove idiom to do that on a vector. Also,
std::list is usually implemented so that insertion at the beginning or
the end takes O(1) time. In fact, IIRC, that's guaranteed by the
standard. If you don't need the random access and you plan on doing a
lot of deleting, I think std::list is the way to go.

Kristo

Oct 4 '05 #7

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

Similar topics

9
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences,...
4
by: Majed | last post by:
hi all i've created a strong named collection which inherits collection base,but when i try to add to it a nullreferenceexception blows. the code is as listed below. do i have to init the list...
11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
6
by: dam_fool_2003 | last post by:
Hai, I thank those who helped me to create a single linked list with int type. Now I wanted to try out for a void* type. Below is the code: #include<stdlib.h> #include<stdio.h>...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
1
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
6
by: APEJMAN | last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result I post the code here I dont wanne bother you, but if any one of you have time to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.