• STL:list用法总结


    一:介绍

    list底层为链表,非连续内存,不支持[]操作符,支持任意位置的插入操作。

    命名空间为std,所属头文件为<list>

    二:常用操作

    容量:
    a.元素个数:list.size()
    b.判断是否为空:list.empty()

    修改:
    a.尾部添加元素:list.push_buack()
    b.首部添加元素:list.push_front()
    c.删除尾部元素:list.pop_back()
    d.删除首部元素:list.pop_front()
    e.插入1:list.insert(pos, num) 在pos位置插入元素num
    f.插入2:list.insert(pos, n, num) 在pos位置插入n个元素num
    g.插入3: list.insert(pos, beg, end) 在pos位置插入另外一块起始位beg到end的元素

    迭代器:
    a.list.begin() 指向链表第一个元素的迭代器
    b.list.end() 指向链表最后一个元素之后的迭代器

    访问:
    a.访问第一个元素:list.front()
    b.访问最后一个元素:list.back()

    删除:
    a.清空:list.clear()
    b.删除指定元素:list.erase(it)

    三:存储

     1     //简单存储
     2     list<int> iList;
     3     iList.push_back(2);        //尾部添加
     4     iList.push_back(3);
     5     iList.push_front(1);    //首部添加
     6 
     7     //存储结构体
     8     list<Student> stuList;
     9     Student stu1;
    10     strcpy(stu1.name, "woniu201");
    11     stu1.age = 30;
    12     Student stu2;
    13     strcpy(stu2.name, "beijing");
    14     stu2.age = 30;
    15     stuList.push_back(stu1);
    16     stuList.push_back(stu2);

    四:遍历

    1     for (list<Student>::iterator it = stuList.begin(); it != stuList.end(); it++)
    2     {
    3         cout << "name:" << it->name << endl;
    4         cout << "age: " << it->age << endl;
    5     }

    五:排序

     1     //升序排序1
     2     iList.sort(less<int>());
     3     for (list<int>::iterator it = iList.begin(); it != iList.end(); it++)
     4     {
     5         cout << *it << endl;
     6     }
     7 
     8     //升序排序2
     9     iList.sort(compare1);
    10     for (list<int>::iterator it = iList.begin(); it != iList.end(); it++)
    11     {
    12         cout << *it << endl;
    13     }
    14 
    15     //降序排序1
    16     iList.sort(greater<int>());
    17     for (list<int>::iterator it = iList.begin(); it != iList.end(); it++)
    18     {
    19         cout << *it << endl;
    20     }
    21 
    22     //降序排序2
    23     iList.sort(compare2);
    24     for (list<int>::iterator it = iList.begin(); it != iList.end(); it++)
    25     {
    26         cout << *it << endl;
    27     }

    六:查找

    1     list<int>::iterator it = find(iList.begin(), iList.end(), 2);
    2     if (it == iList.end())
    3     {
    4         cout << "not found" << endl;
    5     }
    6     else
    7     {
    8         cout << "found"  << endl;
    9     }

    七:删除

     1     for (list<int>::iterator it = iList.begin(); it != iList.end(); it++)
     2     {
     3         if (*it == 2)
     4         {
     5             it = iList.erase(it);
     6             it--;
     7         }
     8     }
     9     for (list<int>::iterator it = iList.begin(); it != iList.end(); it++)
    10     {
    11         cout << *it << endl;
    12     }

    扫码关注公众号

    专注分享Java,C/C++,STL,Spring框架,mybatis框架,mysql,redis,分布式,高并发,设计模式,爬虫,docker,shell编程等相关技术,在这里一起探讨,一起学习,一起进步,不定期分享视频书籍资源,充分利用碎片化时间,让我们的技术之路更加有乐趣。

  • 相关阅读:
    数据中心 CLOS 架构
    CLOS网络的无阻塞条件
    网络层 IP 协议首部格式与其配套使用的四个协议(ARP,RARP,ICMP,IGMP)
    Redis数据库之经典考核习题
    Redis数据库之服务器主从配置
    Redis数据库之KEY的操作与事务管理
    Redis数据库之数据基本管理操作
    Redis数据库安装与配置调试
    基于windows的Redis后台服务安装卸载管理
    面向对象数据模型的构建和分析
  • 原文地址:https://www.cnblogs.com/woniu201/p/9846450.html
Copyright © 2020-2023  润新知