0
点赞
收藏
分享

微信扫一扫

C++ STL list详解

诗远 2022-02-06 阅读 79

写这个博客专栏就是为了以后备用。


简介

        list是一种序列式容器。list容器完成的功能实际上和数据结构中的双向链表是极其相似的,list中的数据元素是通过链表指针串连成逻辑意义上的线性表,list不仅是一个双向链表,而其还是一个环状双向链表。所以它只需要一个指针,便可以完整实现整个链表。list有一个重要性质:插入操作(insert)和合并操作(splice)都不会 造成原有的list迭代器失效。甚至 list的元素删除操作(erase)也只有“指向被删除元素”的那个迭代器失效,其他迭代器不受任何影响。


使用说明

头文件

常用函数


使用示例

#include <iostream>
#include <string>
#include <list>
using namespace std;
typedef list<string> LISTSTR;
int  main()
{
    LISTSTR test;

    test.push_back("back");                         //back
    test.push_front("middle");                      //middle back
    test.push_front("front");                       //front  middle back

    cout<<test.front()<<endl;                       //front
    cout<<*test.begin()<<endl;                      //front
    cout<<test.back()<<endl;                        //back
    cout<<*(test.rbegin())<<endl;                   //back
    test.pop_front();                               //middle back
    test.pop_back();                                //middle
    cout<<test.front()<<endl;                       //middle

    cout<<test.empty()<<endl;                       //False
    cout<<test.size()<<endl;                        //1

    return 0;
}

         这个例子呢,在其他博主的基础上加了点。后边的注释是运行结果。


举报

相关推荐

0 条评论