stl :: list modified erase [英] stl::list modified erase
问题描述
我正在使用多个不同类型的stl :: list。
现在我想写一个函数
list< type> :: iterator s_erase(list< typel,list< type> :: iterator it)
采用迭代器和删除它所指向的元素添加,并且
返回指向已删除元素的前任的迭代器。
像这样:
返回l.erase(it) - ;
但我希望这适用于任何包含的类型。澄清:
我有这些清单:
list< cShipShips;
list< cProjectileProjectiles;
list< cAsteroidAsteroids;
我希望我的函数能够处理所有这些列表。这可能吗?
问候
菲利普
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
推荐答案
3月17日,4:52 pm,Philip Mueller< m ... @ alienenperor.DELETETHIS.de>
写道:
On Mar 17, 4:52 pm, Philip Mueller <m...@alienenperor.DELETETHIS.de>
wrote:
你好,
我正在使用多个不同类型的stl :: list。
现在我想写一个函数
>
list< type> :: iterator s_erase(list< typel,list< type> :: iterator it)
接受迭代器并删除它指向的元素添加,并且
返回指向已擦除元素的前任的迭代器。
像这样:
return l.erase(it) - ;
但是我希望这适用于任何包含的类型。澄清:
我有这些清单:
list< cShipShips;
list< cProjectileProjectiles;
list< cAsteroidAsteroids;
我希望我的函数能够处理所有这些列表。这可能吗?
问候
Philip
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
已经有一种方法可以做到这一点:std :: list :: erase
对我来说,你正在制作一个不会与现有方法不同的函数吗?我错过了什么吗?
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?
3月17日下午5:54,Christopher< cp ... @ austin .rr.comwrote:
On Mar 17, 5:54 pm, Christopher <cp...@austin.rr.comwrote:
3月17日下午4:52,Philip Mueller< m ... @ alienenperor.DELETETHIS.de>
写道:
On Mar 17, 4:52 pm, Philip Mueller <m...@alienenperor.DELETETHIS.de>
wrote:
Hi,
我正在使用多个stl ::列出不同类型的。
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)
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) - ;
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;
list<cShipShips;
list<cProjectileProjectiles;
list<cAsteroidAsteroids;
我希望我的函数能够处理所有这些列表。那可能吗?
And I want my function to work on all of these lists. Is that possible?
问候
Philip
Regards
Philip
已经存在执行该操作的方法:std :: list :: erase
听起来你正在制作一个不会与存在的方法不同的函数吗?我错过了什么?b $ b某事?
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?
哦,我看到唯一的区别是std :: list :: erase返回一个迭代器
指向擦除后的元素你希望它指向
之前的元素。
好吧,我认为没有理由你不能减少返回的迭代器,
,只要你检查it = list.begin()情况
为什么甚至为此做一个函数?如果你有一个方便的列表,那么你有它的类型并且可以创建一个迭代器,并在其上调用erase。
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写道:
Kai-Uwe Bux wrote:
当然。定义一个函数模板:
Sure. Define a function template:
[...]
谢谢,这正是我需要的。不幸的是我从来没有在C中使用过
模板,我只是无法获得调用
函数的语法。你能给出一个例子吗?
[...]
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:我认为,这样的函数填补了代码库中一个急需的空白。
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
这篇关于stl :: list modified erase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!