擦除STL List容器中的第n个元素 [英] Erase n-th element in STL List container

查看:372
本文介绍了擦除STL List容器中的第n个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如何删除 STL 列表容器中的第n个元素.
谁能帮助我.

Hi,
How to erase an n-th element in STL List container.
Can Any one help me.

推荐答案

您可以使用^ ] std::list的方法.这是一个示例代码片段:
You can do this by using erase[^] method of std::list. Here is an example code snippet:
std::list<int> mylist;
std::list<int>::iterator it;

// Fill the list here
for (int i=0; i<10; i++) 
    mylist.push_back (i);

// Init the iterator to point the 1-th element
it = mylist.begin();

// Advance to n-th element (for example we want to erase the 6-th 
// element and "it" points to first elemnt, so we have to advance 5 elements)
std::advance(it, 5);

// Erase the 6-th element
mylist.erase(it);



:)



:)


这篇关于擦除STL List容器中的第n个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆