473,748 Members | 4,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::iterato r end = myListObj.remov e(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::iterato r end = myListObj.remov e(myInt);
myListObj.inser t(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 4952
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::iterat or end = myListObj.remov e(myInt);
myListObj.eras e(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::iterat or end = myListObj.remov e(myInt);
myListObj.inse rt(anotherInt);
myListObj.eras e(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<>::re move() returns void;
- std::list<>::in sert() 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**********@Ho me.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::iterato r end = myListObj.remov e(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::iterato r end = myListObj.remov e(myInt);
myListObj.inser t(anotherInt);
std::list::inse rt 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::iterato r end = myListObj.remov e(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::iterato r end = myListObj.remov e(myInt);
Again, if you want that behavior, you need std::remove.
myListObj.inser t(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::iterato r end = myListObj.remov e(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::iterato r end = myListObj.remov e(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.inser t(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
2550
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, but try to use an iterator of a list that you're mutating and watch it all fall to pieces. That is, if you change the length of a section of the list through which the iterator has already passed, it will lose track of where it is. I think...
4
1804
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 myself. any hints...please! thanks all Public Class NewKeys Inherits BaseCollection
11
3098
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
9145
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> #include<string.h> #include<stddef.h> struct node
12
15097
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 should be kept for references to previous element of list. Here is my solution below: struct node {
1
3332
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 to use as an index to the freearray cells). Here is the exact problem specifications: Create an ordered list template class named OLType to implement an ordered list with operations of insert, remove, print, empty, full, size. The storage...
10
6579
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
15553
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 list. So on the list I can go Trevor, Kevin, Allan in a straight row but I can also call out there last name when I am on their first name in the list. Sorry if it doesn't make sense trying to explain best I can. So far I have // list.cpp
6
3262
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 read my program I appriciate it. the program suppose to print a message on the screen #include <iostream> #include <string>
0
8826
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
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
9366
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...
1
9316
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
isladogs
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4597
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...
1
3303
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
2211
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.