• C++之list


    C++之list

    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述

    list的构造

    在这里插入图片描述

    #include 
    #include
    using namespace std;
    
    //打印函数
    void printfList(const list&L)
    {
        for(list::const_iterator it = L.begin();it != L.end();it++)
        {
            cout<<*it<<" ";
        }
        cout<L1;//默认构成
    
        //添加元素
        L1.push_back(10);
        L1.push_back(20);
        L1.push_back(30);
        L1.push_back(40);
    
        //遍历打印输出
        printfList(L1);
    
        //区间构造
        listL2(L1.begin(),L1.end());
        printfList(L2);
    
        //n个元素构造
        listL3(4,100);
        printfList(L3);
    
        //拷贝构造
        listL4(L2);
        printfList(L4);
    }
    
    int main()
    {
        test();
        cout << "Hello World!" << endl;
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    在这里插入图片描述

    list赋值和交换

    在这里插入图片描述

    #include 
    #include
    using namespace std;
    
    //打印函数
    void printfList(const list&L)
    {
        for(list::const_iterator it = L.begin();it != L.end();it++)
        {
            cout<<*it<<" ";
        }
        cout<L1;//默认构成
    
        //添加元素
        L1.push_back(10);
        L1.push_back(20);
        L1.push_back(30);
        L1.push_back(40);
        //遍历打印输出
        printfList(L1);
    
        //赋值
        listL2;
        L2 = L1;//operator =
        printfList(L2);
    
        listL3;
        L3.assign(L2.begin(),L2.end());
        printfList(L3);
    
        listL4;
        L4.assign(4,100);
        printfList(L4);
    
    }
    
    int main()
    {
        test();
        cout << "Hello World!" << endl;
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    在这里插入图片描述

    #include 
    #include
    using namespace std;
    
    //打印函数
    void printfList(const list&L)
    {
        for(list::const_iterator it = L.begin();it != L.end();it++)
        {
            cout<<*it<<" ";
        }
        cout<L1;//默认构成
    
        //添加元素
        L1.push_back(10);
        L1.push_back(20);
        L1.push_back(30);
        L1.push_back(40);
        //遍历打印输出
        printfList(L1);
    
        listL4;
        L4.assign(4,100);
        printfList(L4);
    
        cout<<"交换后"<
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    在这里插入图片描述

    list的大小操作

    在这里插入图片描述

    #include 
    #include
    using namespace std;
    
    //打印函数
    void printfList(const list&L)
    {
        for(list::const_iterator it = L.begin();it != L.end();it++)
        {
            cout<<*it<<" ";
        }
        cout<L1;//默认构成
    
        //添加元素
        L1.push_back(10);
        L1.push_back(20);
        L1.push_back(30);
        L1.push_back(40);
        //遍历打印输出
        printfList(L1);
    
        //判断是否为空
        if(L1.empty())
        {
            cout<<"L1 is empty"<
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    在这里插入图片描述

    list插入和删除

    在这里插入图片描述

    #include 
    #include
    using namespace std;
    
    //打印函数
    void printfList(const list&L)
    {
        for(list::const_iterator it = L.begin();it != L.end();it++)
        {
            cout<<*it<<" ";
        }
        cout<L1;//默认构成
    
        //尾插
        L1.push_back(10);
        L1.push_back(20);
        L1.push_back(30);
        L1.push_back(40);
    
        //头插
        L1.push_front(100);
        L1.push_front(200);
        L1.push_front(300);
        L1.push_front(400);
        //遍历打印输出
        printfList(L1);
    
        //尾删
        L1.pop_back();
        printfList(L1);
        //头删
        L1.pop_front();
        printfList(L1);
    
        //插入
        list::iterator it=L1.begin();
        L1.insert(++it,1000);
        printfList(L1);
    
        //删除
        it = L1.begin();
        L1.erase(++it);
        printfList(L1);
    
        //移除
        L1.push_back(10000);
        L1.push_back(10000);
        L1.push_back(10000);
        L1.push_back(10000);
        printfList(L1);
        L1.remove(10000);
        printfList(L1);
    
        //清空
        L1.clear();
        printfList(L1);
    
    }
    
    int main()
    {
        test();
        cout << "Hello World!" << endl;
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    在这里插入图片描述

    list数据存取

    在这里插入图片描述

    #include 
    #include
    using namespace std;
    
    //打印函数
    void printfList(const list&L)
    {
        for(list::const_iterator it = L.begin();it != L.end();it++)
        {
            cout<<*it<<" ";
        }
        cout<L1;//默认构成
    
        //尾插
        L1.push_back(10);
        L1.push_back(20);
        L1.push_back(30);
        L1.push_back(40);
    
        //遍历打印输出
        printfList(L1);
    
        cout<<"第一个元素为:"<

在这里插入图片描述

list反转和排序

在这里插入图片描述
在这里插入图片描述

#include 
#include
using namespace std;

//打印函数
void printfList(const list&L)
{
    for(list::const_iterator it = L.begin();it != L.end();it++)
    {
        cout<<*it<<" ";
    }
    cout<V2
    return v1>v2;
}

//list容器
void  test()
{
   //创建list容器
    listL1;//默认构成

    //尾插
    L1.push_back(50);
    L1.push_back(20);
    L1.push_back(10);
    L1.push_back(40);

    cout<<"反转前:"<

在这里插入图片描述

  • 相关阅读:
    石化技术杂志石化技术杂志社石化技术编辑部2022年第10期目录
    10. 元组、集合
    从零编写linux0.11 - 第八章 软盘操作
    开发自己的包----(实现自己需要的功能-格式化日期-转义HTML中的特殊字符-还原HTML中的特殊字符---1)
    STM32/N32G455国民科技芯片驱动DS1302时钟---笔记
    【Java基础】Java基础知识
    ExportExcel打印
    sqlyon连接mysql碰到的问题及mysql卸载须注意的地方
    针对我国——国产数据库进行分析
    字符函数和字符串函数
  • 原文地址:https://blog.csdn.net/qq_45159887/article/details/134464639