C++ STL list及函数基本使用介绍


1. list存储结构

请添加图片描述


2. 相关函数使用

2.1 assign

在这里插入图片描述


2.2 back

在这里插入图片描述


2.3 begin

在这里插入图片描述


2.4 empty

在这里插入图片描述


2.5 end

在这里插入图片描述


2.6 erase

在这里插入图片描述


2.7 front

在这里插入图片描述


2.8 get_allocator

在这里插入图片描述


2.9 insert

在这里插入图片描述


2.10 max_size

在这里插入图片描述


2.11 merge

在这里插入图片描述


2.12 pop_back

在这里插入图片描述


2.13 pop_front

在这里插入图片描述


2.14 push_back

在这里插入图片描述


2.15 push_front

在这里插入图片描述


2.16 rbegin

在这里插入图片描述


2.17 remove

在这里插入图片描述


2.18 remove_if

在这里插入图片描述


2.19 rend

在这里插入图片描述


2.20 resize

在这里插入图片描述


2.21 reverse

在这里插入图片描述


2.22 size

在这里插入图片描述


2.23 sort

在这里插入图片描述


2.24 splice

在这里插入图片描述


2.25 swap

在这里插入图片描述


2.26 unique

在这里插入图片描述


3. 部分使用代码示例


max_size、empty、size、push_back、push_front、pop_back、pop_front

    list<int> list_1;
    cout<<list_1.max_size()<<endl;//178956970最大长度
    cout<<list_1.empty()<<endl;//判断是否为空 1 true
    cout<<list_1.size()<<endl;//0
    
    list_1.push_back(6);//6
    list_1.push_back(7);//6 7 尾插
    list_1.push_front(5);//5 6 7 头插
    
    for(list<int>::iterator it1 = list_1.begin();it1!=list_1.end();++it1)
        cout<<*it1<<"  ";// 5 6 7
    cout<<endl;
    
    //逆序输出
    for(list<int>::reverse_iterator it1 = list_1.rbegin();it1!=list_1.rend();++it1)
        cout<<*it1<<"  ";// 7 6 5
    cout<<endl;
    
    list_1.pop_back();//5 6 尾删
    list_1.pop_front();//6 头删
    
    for(list<int>::iterator it1 = list_1.begin();it1!=list_1.end();++it1)
        cout<<*it1<<"  ";// 6
    cout<<endl;

assign、clear

    list<int> list_2(5,2);//初始化大小5,填充数据2,默认填充0
    cout<<list_2.size()<<endl;//5
    
    list_2.assign(10,3);//空间10,赋值3
    cout<<list_2.size()<<endl;
    //遍历
    for(list<int>::iterator it2 = list_2.begin();it2!=list_2.end();++it2)
        cout<<*it2<<"  ";// 3 3 3 3 3 3 3 3 3 3
    cout<<endl;
    
    list_2.clear();//清除
    cout<<list_2.size()<<endl;//0

insert、erase

	int arr_1[6] = {1,2,3,4,5,6};
    list<int> list_3(arr_1,arr_1+6);
    cout<<list_3.size()<<endl;
    //插入
    list<int>::iterator it3_b = list_3.begin();
    list_3.insert(it3_b,100);//在it3前面插入100 类似地list_3.insert(it3,n,100);为在it3前面插入3个100
    //insert插入不会改变迭代器的位置指向
    for(list<int>::iterator it3 = list_3.begin();it3!=list_3.end();++it3)
        cout<<*it3<<"  ";// 100 1 2 3 4 5 6
    cout<<endl;
    
    list_3.erase(it3_b);//擦除it3元素 上面insert插入不会改变迭代器的位置指向
    
    for(list<int>::iterator it3 = list_3.begin();it3!=list_3.end();++it3)
        cout<<*it3<<"  ";// 100 2 3 4 5 6
    cout<<endl;
    
    it3_b = list_3.begin();
    list<int>::iterator it3_e = list_3.end();
    
    list_3.erase(it3_b,it3_e);//擦除元素
    cout<<list_3.size()<<endl;//0

reverse、unique、sort

	int arr_2[7] = {6,3,2,1,4,5,1};
    list<int> list_4(arr_2,arr_2+7);
    
    for(list<int>::iterator it4 = list_4.begin();it4!=list_4.end();++it4)
        cout<<*it4<<"  ";// 6 3 2 1 4 5 1
    cout<<endl;
    
    list_4.reverse();//逆序
    
    for(list<int>::iterator it4 = list_4.begin();it4!=list_4.end();++it4)
        cout<<*it4<<"  ";// 1 5 4 1 2 3 6
    cout<<endl;
    
    list_4.unique();//清除连续且相同的元素 常与sort使用 单独使用可能不会有什么效果 1 5 4 1 2 3 6
    
    list_4.sort();
    
    for(list<int>::iterator it4 = list_4.begin();it4!=list_4.end();++it4)
        cout<<*it4<<"  ";// 1 1 2 3 4 5 6
    cout<<endl;
    
    list_4.unique();
    
    for(list<int>::iterator it4 = list_4.begin();it4!=list_4.end();++it4)
        cout<<*it4<<"  ";// 1 2 3 4 5 6
    cout<<endl;
    
    cout<<list_4.size()<<endl;//6

swap、splice、merge、remove、resize

	int arr_3[5] = {0,1,2,3,4};
    list<int> list_5(arr_3,arr_3+5);
    int arr_4[6] = {5,6,7,8,9,10};
    list<int> list_6(arr_4,arr_4+6);
    
    for(list<int>::iterator it5 = list_5.begin();it5!=list_5.end();++it5)
        cout<<*it5<<"  ";// 0 1 2 3 4
    cout<<endl;
    cout<<list_5.size()<<endl;//5
    for(list<int>::iterator it6 = list_6.begin();it6!=list_6.end();++it6)
        cout<<*it6<<"  ";// 5 6 7 8 9 10
    cout<<endl;
    cout<<list_6.size()<<endl;//6
    
    //链表交换
    list_5.swap(list_6);
    for(list<int>::iterator it5 = list_5.begin();it5!=list_5.end();++it5)
        cout<<*it5<<"  ";// 5 6 7 8 9 10
    cout<<endl;
    cout<<list_5.size()<<endl;//6
    for(list<int>::iterator it6 = list_6.begin();it6!=list_6.end();++it6)
        cout<<*it6<<"  ";// 0 1 2 3 4
    cout<<endl;
    cout<<list_6.size()<<endl;//5
    
    //链表连接 类似头插
    list<int>::iterator it6 = list_6.begin();
    list_6.splice(it6,list_5);//将list_5合并到it6之前 也可使用迭代器指定合并的序列
    for(list<int>::iterator it6 = list_6.begin();it6!=list_6.end();++it6)
        cout<<*it6<<"  ";// 5 6 7 8 9 10 0 1 2 3 4
    cout<<endl;
    cout<<list_6.size()<<endl;//11
    cout<<list_5.size()<<endl;//0 list_5被清空
    
    //链表合并 类似尾插
    list_5.assign(arr_4,arr_4+6);// 5 6 7 8 9 10
    list_6.sort();// 0 1 2 3 4 5 6 7 8 9 10
    list_6.merge(list_5);//必须先保证两个链表有序
    for(list<int>::iterator it6 = list_6.begin();it6!=list_6.end();++it6)
        cout<<*it6<<"  ";// 0 1 2 3 4 5 5 6 6 7 7 8 8 9 9 10 10
    cout<<endl;
    cout<<list_6.size()<<endl;//17
    cout<<list_5.size()<<endl;//0 list_5被清空

    list_6.resize(3,0);//重置大小,多的加入val值
    for(list<int>::iterator it6 = list_6.begin();it6!=list_6.end();++it6)
        cout<<*it6<<"  ";// 0 1 2
    cout<<endl;

	//remove
    int arr_5[5] = {1,2,3,1,2};
    list<int> list_7(arr_5,arr_5+5);
    list_7.remove(1);
    cout<<list_7.size()<<endl;//3
    list_7.remove(2);
    cout<<list_7.size()<<endl;//1
    list_7.remove(3);
    cout<<list_7.size()<<endl;//0

const_iterator使用

    int arr_6[4] = {0,1,2,3};
    const list<int> list_8(arr_6,arr_6+4);//const遍历
    for(list<int>::const_iterator it8 = list_8.begin();it8!=list_8.end();++it8)
        cout<<*it8<<"  ";// 0 1 2 3
    cout<<endl;

——————END-2022-04-22——————
————————感谢您的阅读————————

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苡荏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值