list学习之删除操作clear,erase, pop_front, pop_back

本篇学习list的删除操作

clear:清除内容

erase:擦除元素

pop_front:移除首元素

pop_back:移除末元素

代码实现

#include <list>
#include <iostream>
#include <sstream>
#include <time.h>

using namespace std;

void deleteElement()
{
    //1.clear擦除内容
    list<int> list1 = {22, 25, 27, 30, 33};
    cout << "list1 的值: ";
    for(auto &val: list1)
        cout << val << "\t";
    cout << endl;
    cout << "list1.size = " << list1.size() << endl;
    list1.clear();
    cout << "list1.size = " << list1.size() << endl;

    //2.erase:擦除元素
    list<int> list2 = {36, 39, 41,  42};
    auto it1 = list2.erase(list2.begin());//擦除首元素,即删除36,返回25
    cout << "it1 = " << *it1 << endl;
    cout << "list2 的值: ";
    for(auto &val: list2)
        cout << val << "\t";
    cout << endl;
    //擦除范围
    list<int> list3 = {43, 44, 45, 46, 47, 48, 49};
    auto begin = std::next(list3.begin());
    auto end = std::next(list3.begin(), 4);
    --end;
    cout << "begin = " << *begin << endl;
    cout << "end   = " << *end << endl;
    cout << "list3 的值: ";
    for(auto &val: list3)
        cout << val << "\t";
    cout << endl;

    //3.pop_front:移除首元素
    list3.pop_front();//无返回值
    cout << "list3 的值: ";
    for(auto &val: list3)
        cout << val << "\t";
    cout << endl;
    //4.pop_back:移除末元素
    list<int> list4 = {50, 51, 52, 53};
    cout << "list4 的值: ";
    for(auto &val: list4)
        cout << val << "\t";
    cout << endl;
    list4.pop_back();
    cout << "list4 的值: ";
    for(auto &val: list4)
        cout << val << "\t";
    cout << endl;
    cout << endl;
}

int main()
{
    deleteElement();
    cout << endl;

    cout << " Hello World!" << endl;
    return 0;
}

运行结果:

参考:

http://www.cplusplus.com/reference/list/list/
https://zh.cppreference.com/w/cpp/container/list

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C++ 中,list 是双向链表容器,可以用来存储和操作相同类型的数据。list 支持在任意位置插入/删除元素,但是不支持随机访问元素。 以下是 list 的一些常用操作: 1. push_front():在链表头部插入元素。 2. push_back():在链表尾部插入元素。 3. pop_front():删除链表头部元素。 4. pop_back():删除链表尾部元素。 5. insert():在指定位置插入元素。 6. erase():删除指定位置的元素。 7. clear():清空整个链表。 8. size():返回链表中元素的个数。 9. empty():判断链表是否为空。 以下是 list 的一个简单示例: ```c++ #include <iostream> #include <list> using namespace std; int main() { list<int> mylist; // 在链表尾部插入元素 mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); // 在链表头部插入元素 mylist.push_front(0); // 遍历链表并输出元素 for (auto it = mylist.begin(); it != mylist.end(); ++it) cout << *it << " "; cout << endl; // 删除链表头部元素 mylist.pop_front(); // 在指定位置插入元素 auto it = mylist.begin(); ++it; mylist.insert(it, 10); // 删除指定位置的元素 it = mylist.begin(); ++it; mylist.erase(it); // 遍历链表并输出元素 for (auto x : mylist) cout << x << " "; cout << endl; // 清空整个链表 mylist.clear(); // 判断链表是否为空 if (mylist.empty()) cout << "The list is empty." << endl; return 0; } ``` 输出结果: ``` 0 1 2 3 0 2 3 10 The list is empty. ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值