从集合中删除特定元素 [英] remove specific element from a set
问题描述
大家好,
这可能是一个简单的问题,但也许有些人可以说答案。
我正在寻找一个允许从集合中删除*特定*元素的数据结构?我应该使用哪种数据结构?
例如使用堆栈对我来说不太合适,因为它只允许删除* top *元素,而不是元素说它位于中间。
如果我使用数组,并删除中间说一个元素,那么我必须重新排列数组中的元素,以便我把一个现有的元素放在我删除的元素的位置;我希望数据结构自动完成。
希望我很清楚。谢谢!
Hi people,
this maybe an easy question but maybe some of you can say the answer.
I am looking for a data structure which allows to delete a *specific* element from a set? which data structure should I use for it?
For example using stack is not quite right for me because it allows to delete only the *top* element, not element say which is located in the middle.
if I use an array, and delete say an element in the middle, then I have to rearrange the elements in the array so that I put an existing element in the place of the element I deleted; I want the data structure to do this automatically.
hope i was clear. thanks!
推荐答案
您可以使用STL中的设置模板。
http://www.cplusplus.com/reference/set/set/ [ ^ ]
You may us the set template in STL.
http://www.cplusplus.com/reference/set/set/[^]
Modifiers:
insert
Insert element (public member function)
erase
Erase elements (public member function)
有一些示例代码:
http://www.cplusplus.com/reference/set/set/erase/ [ ^ ]
您可以从近乎删除一个随机元素所有容器类,但不是来自队列或堆栈。它可能是列表,矢量,地图等。我没有计算本机C / C ++数组,它不是容器类。你应该选择哪种容器类?这取决于您的其他要求。
-SA
You can remove a random element from nearly all container classes, but not from a queue or a stack. It could be list, vector, map, etc. I did not count native C/C++ array, which is not a container class. What container class should you choose? It depends on your other requirements.
—SA
这篇关于从集合中删除特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!