Qt使用std::list删除指定位置元素

删除指定位置语句为:

L.erase(pos)

其中L为链表对象,pos为链表中元素指针,因为链表不像顺序表一样可以随机存储,要遍历到指定位置才能存储,

所以可通过for循环来定位pos,程序如下:

 1     std::list<int> l;
 2     l.push_back(1);
 3     l.push_back(2);
 4     l.push_back(3);
 5     l.push_back(4);
 6     l.push_back(5);
 7 
 8     auto p= l.begin();
 9     for (int i = 0; i < 2; i++)  //  假如要删除第三个元素
10     {
11         p++;
12     }
13     qDebug()<<*p;
14     
15     l.erase(p);  //  删除指定位置元素
16     for (auto it = l.begin(); it != l.end() ; it++ )
17     {
18         qDebug()<<*it;
19     }

 

posted @ 2021-05-13 19:17  补码  阅读(932)  评论(0编辑  收藏  举报