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 the element it is pointing add, and
returns the iterator pointing to the predecessor of the erased element.
like this:
return l.erase(it)--;
But I want this to work for any contained type. For clarification:
I have theses lists:
list<cShipShips;
list<cProjectileProjectiles;
list<cAsteroidAsteroids;
And I want my function to work on all of these lists. Is that possible?
Regards
Philip 12 2268
On Mar 17, 4:52 pm, Philip Mueller <m...@alienenperor.DELETETHIS.de>
wrote:
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 the element it is pointing add, and
returns the iterator pointing to the predecessor of the erased element.
like this:
return l.erase(it)--;
But I want this to work for any contained type. For clarification:
I have theses lists:
list<cShipShips;
list<cProjectileProjectiles;
list<cAsteroidAsteroids;
And I want my function to work on all of these lists. Is that possible?
Regards
Philip
There already exists a method that does that: std::list::erase
It sounds to me that you are making a function that does not do
anything differently than the method that exists? Am I missing
something?
On Mar 17, 5:54 pm, Christopher <cp...@austin.rr.comwrote:
On Mar 17, 4:52 pm, Philip Mueller <m...@alienenperor.DELETETHIS.de>
wrote:
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 the element it is pointing add, and
returns the iterator pointing to the predecessor of the erased element.
like this:
return l.erase(it)--;
But I want this to work for any contained type. For clarification:
I have theses lists:
list<cShipShips;
list<cProjectileProjectiles;
list<cAsteroidAsteroids;
And I want my function to work on all of these lists. Is that possible?
Regards
Philip
There already exists a method that does that: std::list::erase
It sounds to me that you are making a function that does not do
anything differently than the method that exists? Am I missing
something?
Oh, I see the only difference is std::list::erase return an iterator
pointing to the element after the erased and you want it to point to
the element before.
Well, I see no reason you could not decrement the returned iterator,
as long as you check for the it = list.begin() case
Why even make a function for that? If you have the list handy, than
you have its type and can make an iterator, and call erase on it.
Kai-Uwe Bux wrote:
Sure. Define a function template:
[...]
Thanks, that's just what I need. Unfortunately I have never worked with
templates in C and I just can't get the syntax for a call to that
function right. Could you give an example?
BTW: I think, such a function fills a much needed gap in your code base.
Yeah, it would really make the project look a whole lot cleaner :-)
Regards
Philip
Philip Mueller wrote:
Kai-Uwe Bux wrote:
>Sure. Define a function template:
[...]
Please, don't snip context that you are refering to:
template < typename ListType >
typename ListType::iterator
s_erase ( ListType & the_list,
typename ListType::iterator iter ) {
...
}
Thanks, that's just what I need. Unfortunately I have never worked with
templates in C and I just can't get the syntax for a call to that
function right. Could you give an example?
Since all types can be deduced from the arguments, the following should
suffice:
s_erase( my_list, my_iter );
>BTW: I think, such a function fills a much needed gap in your code base.
Yeah, it would really make the project look a whole lot cleaner :-)
Either you are great master of irony, or my hint was just too subtle.
Best
Kai-Uwe Bux
Kai-Uwe Bux schrieb:
Philip Mueller wrote:
>Kai-Uwe Bux wrote:
>>Sure. Define a function template:
template < typename ListType >
typename ListType::iterator
s_erase ( ListType & the_list,
typename ListType::iterator iter ) {
...
}
>Unfortunately I have never worked with templates in C and I just can't get the syntax for a call to that function right. Could you give an example?
Since all types can be deduced from the arguments, the following should
suffice:
s_erase( my_list, my_iter );
When I do this
list<intTest;
list<int>::iterator iter;
iter = s_erase(Test, iter);
I get a compile error saying
undefined reference to `std::list<int, std::allocator<int::iterator
s_erase<std::list<int, std::allocator<int >(std::list<int,
std::allocator<int&, std::list<int, std::allocator<int::iterator)'
I find this very difficult to read, but even after reading it multiple
times I don't understand it.
Do you know where I can find introductory texts about templates?
>>BTW: I think, such a function fills a much needed gap in your code base.
Yeah, it would really make the project look a whole lot cleaner :-)
Either you are great master of irony, or my hint was just too subtle.
Maybe it was.
For clarification:
I need s_erase because I have a for-loop iterating over the list and
deleting objects that satisfy a given condition.[1]
Now, when I use the stl::list "erase()" method, it returns an iterator
to the next element.
In the next iteration the loop also increments the iterator so I have
skipped the Element after the one I deleted.
If you have a different, simpler idea, you're welcome to share it with
me :-)
Regards
Philip
[1]:
list<cShipShips;
for (list<cShip>::iterator iS = Ships.begin(); iS != Ships.end(); iS++)
{
//This is the version that skips elements. I don't want that :-)
if (condition(iS))
iS = Ships.erase(iS);
}
Another possibility would be
if (condition(iS))
{
iS = Ships.erase(iS);
iS--;
}
but I don't think that's good code, especially because I need that
fragment at multiple locations in the program.
That's why I want a method for that.
if (condition(iS)) iS = s_erase(Ships, iS);
Philip Mueller wrote:
Kai-Uwe Bux schrieb:
>Philip Mueller wrote:
>>Kai-Uwe Bux wrote: Sure. Define a function template:
template < typename ListType > typename ListType::iterator s_erase ( ListType & the_list, typename ListType::iterator iter ) { ... }
>>Unfortunately I have never worked with templates in C and I just can't get the syntax for a call to that function right. Could you give an example?
Since all types can be deduced from the arguments, the following should suffice:
s_erase( my_list, my_iter );
When I do this
list<intTest;
list<int>::iterator iter;
iter = s_erase(Test, iter);
I get a compile error saying
undefined reference to `std::list<int, std::allocator<int::iterator
s_erase<std::list<int, std::allocator<int >(std::list<int,
std::allocator<int&, std::list<int, std::allocator<int::iterator)'
Did you put the declaration of s_erase in a header and the implementation in
a cpp-file? If so, you would need a compiler that implements the export
keyword. For compilers that don't (which is the case for most of them),
template implementations need to go into the header file.
I find this very difficult to read, but even after reading it multiple
times I don't understand it.
Do you know where I can find introductory texts about templates?
I use
Vandervoorde and Josuttis: "C++ Templates: The Complete Guide"
but I don't know if that qualifies as an introductory text.
>>>BTW: I think, such a function fills a much needed gap in your code base. Yeah, it would really make the project look a whole lot cleaner :-)
Either you are great master of irony, or my hint was just too subtle.
Maybe it was.
For clarification:
I need s_erase because I have a for-loop iterating over the list and
deleting objects that satisfy a given condition.[1]
Now, when I use the stl::list "erase()" method, it returns an iterator
to the next element.
In the next iteration the loop also increments the iterator so I have
skipped the Element after the one I deleted.
If you have a different, simpler idea, you're welcome to share it with
me :-)
The standard idioms to remove elements from a list is:
for ( iterator iter = my_list.begin(); iter != my_list.end(); ) {
if ( condition( *iter ) ) {
iter = my_list.erase( iter );
} else {
++ iter;
}
}
More concisely but requiring a function or function object you can use the
remove_if() member function:
my_list.remove_if( predicate_object );
Regards
Philip
[1]:
list<cShipShips;
for (list<cShip>::iterator iS = Ships.begin(); iS != Ships.end(); iS++)
{
//This is the version that skips elements. I don't want that :-)
if (condition(iS))
iS = Ships.erase(iS);
}
Another possibility would be
if (condition(iS))
{
iS = Ships.erase(iS);
iS--;
}
but I don't think that's good code, especially because I need that
fragment at multiple locations in the program.
That's why I want a method for that.
if (condition(iS)) iS = s_erase(Ships, iS);
The problem with those alternative versions is that they have some trouble
deleting the first element of the list.
Best
Kai-Uwe Bux
In article <64*************@mid.uni-berlin.de>,
Philip Mueller <me@alienenperor.DELETETHIS.dewrote:
Kai-Uwe Bux schrieb:
Philip Mueller wrote:
Kai-Uwe Bux wrote: Sure. Define a function template:
template < typename ListType >
typename ListType::iterator
s_erase ( ListType & the_list,
typename ListType::iterator iter ) {
...
}
Unfortunately I have never worked with
templates in C and I just can't get the syntax for a call to that
function right. Could you give an example?
Since all types can be deduced from the arguments, the following should
suffice:
s_erase( my_list, my_iter );
When I do this
list<intTest;
list<int>::iterator iter;
iter = s_erase(Test, iter);
I get a compile error saying
undefined reference to `std::list<int, std::allocator<int::iterator
s_erase<std::list<int, std::allocator<int >(std::list<int,
std::allocator<int&, std::list<int, std::allocator<int::iterator)'
I find this very difficult to read, but even after reading it multiple
times I don't understand it.
Do you know where I can find introductory texts about templates?
My guess is it is complaining because you put the definition of the
function in a cpp file other than the one using it. Take the code that
Kai gave you and paste it directly in a .h file.
>BTW: I think, such a function fills a much needed gap in your code base.
Yeah, it would really make the project look a whole lot cleaner :-)
Either you are great master of irony, or my hint was just too subtle.
Maybe it was.
For clarification:
I need s_erase because I have a for-loop iterating over the list and
deleting objects that satisfy a given condition.[1]
Now, when I use the stl::list "erase()" method, it returns an iterator
to the next element.
In the next iteration the loop also increments the iterator so I have
skipped the Element after the one I deleted.
If you have a different, simpler idea, you're welcome to share it with
me :-)
Ships.remove_if( &condition );
The above will do what you describe below without much effort on your
part. You might have to modify condition to take a const reference
rather than a pointer.
[1]:
list<cShipShips;
for (list<cShip>::iterator iS = Ships.begin(); iS != Ships.end(); )
{
if (condition(iS))
iS = Ships.erase(iS);
else
++iS;
}
I modified the code above so that it doesn't skip elements anymore.
Philip Mueller wrote:
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 the element it is pointing add, and
returns the iterator pointing to the predecessor of the erased
element.
like this:
return l.erase(it)--;
But I want this to work for any contained type. For clarification:
I have theses lists:
list<cShipShips;
list<cProjectileProjectiles;
list<cAsteroidAsteroids;
And I want my function to work on all of these lists. Is that
possible?
You need to work around the possibility of your 'l's being empty,
or containing only one element (after which you're trying to use
post-decrement on the 'end', which is UB), but beyond that, what
is stopping you from making it a template?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Kai-Uwe Bux wrote:
The standard idioms to remove elements from a list is:
for ( iterator iter = my_list.begin(); iter != my_list.end(); ) {
if ( condition( *iter ) ) {
iter = my_list.erase( iter );
} else {
++ iter;
}
}
First of all, thanks for your suggestions Kai-Uwe.
This example makes perfect sense to me. But I have problems adapting it
to a special case:
for (iProj = Projectiles.begin(); iProj != Projectiles.end(); iProj++)
for (iAst = Asteroids.begin(); iAst != Asteroids.end(); iAst++)
if (iProj->DistanceTo(&*iAst) <= iAst->getRadius())
{
iProj = Projectiles.erase(iProj);
iAst = Asteroids.erase(iAst);
}
What I mean to do here is test two list of objects for collisions
between an element from the "Projectiles" list and an element from the
"Asteroids" list (Collision detection via "DistanceTo").
Then if there is such a collision, I want to delete both the projectile
and the asteroid.
Can someone give me a hint?
Regards
Philip
p.s.:
My best try so far is
for (iProj = Projectiles.begin(); iProj != Projectiles.end();)
{
iAst = Asteroids.begin();
while (iAst != Asteroids.end()
&& iProj->DistanceTo(&*iAst) iAst->getRadius())
iAst++;
if (iAst != Asteroids.end())
{
iAst = Asteroids.erase(iAst);
iProj = Projectiles.erase(iProj);
}
else
iProj++;
}
There has to be a better way.
Philip Mueller wrote:
Kai-Uwe Bux wrote:
>The standard idioms to remove elements from a list is:
for ( iterator iter = my_list.begin(); iter != my_list.end(); ) { if ( condition( *iter ) ) { iter = my_list.erase( iter ); } else { ++ iter; } }
First of all, thanks for your suggestions Kai-Uwe.
This example makes perfect sense to me. But I have problems adapting it
to a special case:
for (iProj = Projectiles.begin(); iProj != Projectiles.end(); iProj++)
for (iAst = Asteroids.begin(); iAst != Asteroids.end(); iAst++)
if (iProj->DistanceTo(&*iAst) <= iAst->getRadius())
{
iProj = Projectiles.erase(iProj);
iAst = Asteroids.erase(iAst);
}
What I mean to do here is test two list of objects for collisions
between an element from the "Projectiles" list and an element from the
"Asteroids" list (Collision detection via "DistanceTo").
Then if there is such a collision, I want to delete both the projectile
and the asteroid.
Can someone give me a hint?
Would this work:
bool hits_asteroid ( Projectile const & p,
AsteroidList & Asteroids ) {
for ( AsteroidList::iterator iAst = Asteroids.begin();
iAst != Asteroids.end() ) {
if ( p.DistanceTo(&*iAst) <= iAst->getRadius() ) {
iAst = Asteroids.erase( iAst );
return ( true );
} else {
++iAst;
}
}
return ( false );
}
and then:
for ( iProj = Projectiles.begin(); iProj != Projectiles.end() ) {
if ( hits_asteroid( *iProj, Asteroids ) ) {
iProj = Projectiles.erase( iProj );
} else {
++ iProj;
}
}
If you want to fold that into a double loop (e.g., if you don't like
predicates with side effects), I would think that the best way is to have a
boolean flag that keeps track whether an explosion has happened.
Best
Kai-Uwe Bux
Kai-Uwe Bux wrote:
Would this work:
bool hits_asteroid ( Projectile const & p,
AsteroidList & Asteroids ) {
for ( AsteroidList::iterator iAst = Asteroids.begin();
iAst != Asteroids.end() ) {
if ( p.DistanceTo(&*iAst) <= iAst->getRadius() ) {
iAst = Asteroids.erase( iAst );
return ( true );
} else {
++iAst;
}
}
return ( false );
}
and then:
for ( iProj = Projectiles.begin(); iProj != Projectiles.end() ) {
if ( hits_asteroid( *iProj, Asteroids ) ) {
iProj = Projectiles.erase( iProj );
} else {
++ iProj;
}
}
If you want to fold that into a double loop (e.g., if you don't like
predicates with side effects), I would think that the best way is to have a
boolean flag that keeps track whether an explosion has happened.
Just for others who might have had the same problems:
I implemented the version with the boolean "explode" flag, but that one
seemed too complex, so I thought about it again and came up with a new
solution which, IMO, is the best so far:
All my Objects get new member functions setToBeDeleted() and
isToBeDeleted() and I defined a predicate
bool isToBeDeleted(cSpaceObject& obj)
{
return obj.isToBeDeleted();
}
so that now the loop looks like this:
for (iProj = Projectiles.begin(); iProj != Projectiles.end(); iProj++)
for (iAst = Asteroids.begin(); iAst != Asteroids.end(); iAst++)
if (iProj->DistanceTo(&*iAst) <= iAst->getRadius())
{
iAst->setToBeDeleted();
iProj->setToBeDeleted();
}
Asteroids.remove_if(isToBeDeleted);
Projectiles.remove_if(isToBeDeleted);
Thanks again for all the constructive input.
Regards
Philip
Philip Mueller wrote:
Kai-Uwe Bux wrote:
>Would this work:
bool hits_asteroid ( Projectile const & p, AsteroidList & Asteroids ) { for ( AsteroidList::iterator iAst = Asteroids.begin(); iAst != Asteroids.end() ) { if ( p.DistanceTo(&*iAst) <= iAst->getRadius() ) { iAst = Asteroids.erase( iAst ); return ( true ); } else { ++iAst; } } return ( false ); }
and then:
for ( iProj = Projectiles.begin(); iProj != Projectiles.end() ) { if ( hits_asteroid( *iProj, Asteroids ) ) { iProj = Projectiles.erase( iProj ); } else { ++ iProj; } }
If you want to fold that into a double loop (e.g., if you don't like predicates with side effects), I would think that the best way is to have a boolean flag that keeps track whether an explosion has happened.
Just for others who might have had the same problems:
I implemented the version with the boolean "explode" flag, but that one
seemed too complex, so I thought about it again and came up with a new
solution which, IMO, is the best so far:
All my Objects get new member functions setToBeDeleted() and
isToBeDeleted() and I defined a predicate
bool isToBeDeleted(cSpaceObject& obj)
{
return obj.isToBeDeleted();
}
so that now the loop looks like this:
for (iProj = Projectiles.begin(); iProj != Projectiles.end(); iProj++)
for (iAst = Asteroids.begin(); iAst != Asteroids.end(); iAst++)
if (iProj->DistanceTo(&*iAst) <= iAst->getRadius())
{
iAst->setToBeDeleted();
iProj->setToBeDeleted();
}
Asteroids.remove_if(isToBeDeleted);
Projectiles.remove_if(isToBeDeleted);
Thanks again for all the constructive input.
Note that this has different semantics: in this version, one projectile can
take out several asteroids and an asteroid can be hit by several
projectiles.
Best
Kai-Uwe Bux This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Paras |
last post by:
Hi
What is the correct way to delete an element from STL list while
iterating through the list
list<A*> _ls;
A * a;
list<A*>::iterator si1;
|
by: Barry Hynes |
last post by:
G'Day folks,
Have been working on this problem for quite some time and still no farther
ahead. :(
Here is my problem...bare with me i am very green :)
I have to implement a Safe List,...
|
by: Christian Christmann |
last post by:
Hi,
I need an STL list and was thinking of putting
the list in wrapper class.
The reason for my decision is that you can
much better perform consistency checks.
For instance, I need a...
|
by: cpptutor2000 |
last post by:
I am using an STL list to store data packets in a network simulator.
The data packets are really C structs, which have other C structs
inside them. These structs contain
unsigned char arrays of...
|
by: Allerdyce.John |
last post by:
In STL list, is it safe to do this:
list<A> aList; //private attribute of MyClass
void MyClass:: aMethod() {
list<A>::Iterator iter;
for ( iter = aList.begin() ; iter != aList.end() ;...
|
by: Allerdyce.John |
last post by:
Hi,
How can I Random access an element in a STL List if I have a pointer to
that list?
I know how to do that if I have a reference to a STL list, but how can
I do that if I have a pointer to a...
|
by: Jonathan |
last post by:
Hi. I'm having trouble figuring out what I should be doing here.
I'm trying to remove an object from a list.
The function is:
void Alive::FromRoom ()
{
list<Alive>::iterator iter =...
|
by: Christian Christmann |
last post by:
Hi,
reading the output of gprof for one of my projects, I found that the
STL list assignment operator consumes a larger fraction of the
program's execution time. The exact entry in gprof's...
|
by: Boltar |
last post by:
Hello
I need to iterate through an STL list container and delete certain
entries in it. I realise if use erase() with the iterator it will
invalidate it but what if I store it beforehand? Ie: is...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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...
| |