List 容器 -----C++的stl学习

一.原理:

list容器的本质就是 对 链表结构的封装 , 对链表有疑问的读者可以 阅读一下这一篇文章:

从零开始的数据结构------2.链表 ---- 单链表实现-CSDN博客

我们就直接步入正题来讲讲list容器常见的api,具体的运用吧!!!

二.常见api

•构造函数:

list<T> list //list 采用模板类实现, 对象的默认构造形式

list(beg,end) //构造函数将(beg,end)区间中的元素拷贝给本身。

list(n,elem) //构造函数将n个elem来初始化元素

list(const list &lt) //纯粹的拷贝构造

list(beg,end)中的 beg 和 end 都指的是具体的迭代器,例如:begin()或者end()等等。

cpp 复制代码
#include<iostream>
#include<list>
using namespace  std;

void printList(const list<int> &lt){
    for (list<int> :: const_iterator it = lt.begin(); it != lt.end(); it++){
        cout << *it << " ";
    }
    cout << endl;
}

void test01(){
   list<int> lt;

   lt.push_back(10);
   lt.push_back(20);
   lt.push_back(30);
   printList(lt);

   //区间构造
   list<int> m(lt.begin(),lt.end());
   printList(m);

   //初始化
   list<int> j(5,100);
   printList(j);

   //纯粹的拷贝构造:
   list<int> cc(j);
   printList(cc);
}

int main(){
   test01();
   system("pause");

   return 0;
}

运行结果是:

10 20 30

10 20 30

100 100 100 100 100

100 100 100 100 100

请按任意键继续. . .

• 赋值和交换:

assign(beg,end) //将beg 和 end 中的数据拷贝赋值给本身

assign (n,elem) //将n个值赋值给几个元素

list& operator * (const list &list) //重载等号操作符

swap(list) //将元素互换

常见的是***= 赋值符 ,拷贝赋值***

++assign 用于解决初始化后,想要重新赋值的需求++

cpp 复制代码
#include<iostream>
#include<list>
using namespace  std;

void printList(const list<int> &lt){
    for (list<int> :: const_iterator it = lt.begin(); it != lt.end(); it++){
        cout << *it << " ";
    }
    cout << endl;
}

void test01(){
   list<int> lt;

   lt.push_back(10);
   lt.push_back(20);
   lt.push_back(30);
   printList(lt);

   list<int> lt2;
   lt2 = lt;
   printList(lt2);


   list<int> lt3;
   lt3.assign(lt2.begin(),lt2.end());
   printList(lt3);

   list<int> lt4;
   lt4.assign(10,100);
   printList(lt4);

   swap(lt,lt4);
   printList(lt4);
   
}

int main(){
   test01();
   system("pause");

   return 0;
}

运行结果:

10 20 30

10 20 30

10 20 30

100 100 100 100 100 100 100 100 100 100

10 20 30

请按任意键继续. . .

•大小操作:

size() //返回容器中实际存储的元素个数

empty() //判断容器是否为空

resize( n ) //重新指定元素实际存储个数为n(影响size())

resize( n ,elm ) // 重新指定容器实际储存个数为 n ,同时这些更改的元素值初始化为elm

• 插入和删除

push_back() // 在容器尾部加入一个元素

pop_back() //删除容器中的最后一个元素

push_front() //在容器开头插入一个元素

pop_front() //在容器开头移除第一个元素

insert(pos,elem) //在容器位置插入elem元素的拷贝,返回新的数据

insert(pos,n,elem) //在pos位置插入 n个 elem数据,无返回值

insert(pos,beg,end) //在pos位置插入【beg,end)区间的数据,无返回值

clear() //移除容器的所有数据

erase(beg,end) //删除(beg,end)区间的数据,返回下一个数的位置

erase(pos) //删除pos位置的数据,返回下一个数据

remove(elem) //移除容器中所有和elem匹配的元素

insert 和 erase 尤其好用,能够自动 插入/删除 节点.

原始人编程" 中,我们更改链表需要:

通过调用api 就可以自动帮我们实现这两个操作。

cpp 复制代码
#include<iostream>
#include<list>
using namespace std;

void printList(const list<int> &L){
   for (list<int> :: const_iterator it = L.begin(); it != L.end(); it++){
          cout << *it << " ";
   }
   cout << endl;
}


void test(){
   list<int> L;

   //尾部插入:
   L.push_back(10);
   L.push_back(20);
   L.push_back(30);

   //头插法
   L.push_front(3);
   L.push_front(2);
   L.push_front(1);
   
   printList(L);

   //尾部删除
   L.pop_back();
   printList(L);

   //头部删除
   L.pop_front();
   printList(L);

   //insert插入
   list<int>:: iterator it = L.begin();
   it++;
   L.insert(it,100);
   printList(L);

   //删除
   it = L.end();
   it--;
   it--;
   L.erase(it);
   printList(L);

   //清空
   L.clear();  
   printList(L);

}

int main(){
   test();
   system("pause");
   return 0;
}

运行结果:

1 2 3 10 20 30

1 2 3 10 20

2 3 10 20

2 100 3 10 20

2 100 3 20

请按任意键继续. . .

• 数据存取:

front() // 返回第一个数据

back() // 返回最后一个数据

由链表的底层逻辑影响,不能像数组那样,"跳跃式"地访问数据,只能从头或者从尾循环访问。

例如:

迭代器 list<T> :: iterator it = L.begin() --- L是list变量名

支持 it++ 或者 it-- ,能够逐个前后移动。

但是不支持 it = it + 3 这种跳跃式的访问

•链表的排序和反转

reverse() //链表的反准

sort() //链表排序

cpp 复制代码
#include<iostream>
#include<list>
using namespace std;

void printLIst( const list<int> &L){
    for (list<int> :: const_iterator it = L.begin(); it != L.end(); it++){
          cout << *it << " ";
    }
    cout << endl;
}


void test(){
    list<int> L;
    
    //插入一些无序的数字
    L.push_back(20);
    L.push_back(10);
    L.push_back(30);
    cout << "反转前" << endl;
    printLIst(L);

    //反转
    cout << "反转后" << endl;
    L.reverse();
    printLIst(L);

    //排序
    L.sort();
    cout << "排序后" << endl;
    printLIst(L);
}

int main(){
    test();
    system("pause"); 
    return 0;
}
相关推荐
-银雾鸢尾-16 小时前
C#中的拓展方法
开发语言·c#
guodingdingh18 小时前
软件开发工作问题总结0718
java·开发语言·数据库
世人万千丶20 小时前
鸿蒙Flutter Flex多子组件权重分配
学习·flutter·华为·harmonyos·鸿蒙
优橙教育20 小时前
5G网优培训 vs Java开发:转行选哪个?
java·开发语言·5g
同勉共进21 小时前
记一例 vibe coding + gcc bug 导致的线程池死锁问题
c++·线程池·gcc·死锁·vibe coding
我叫洋洋1 天前
C ++ [ hello world ]
c语言·c++·算法
SeaTunnel1 天前
从 Python Script 地狱到标准化数据集成框架
大数据·开发语言·python·程序员·代码·seatunnel
碎光拾影1 天前
ARM交叉工具链各工具作用及IMX6ULL平台LED+蜂鸣器裸机程序实现
java·开发语言·数据库
圣光SG1 天前
Servlet学习笔记
笔记·学习·servlet
Marst Code1 天前
(python)2026Plotly 库评估:交互式可视化到底值不值得引入?
开发语言·python