【嵌入式——C++】list(STL)

【嵌入式------C++】 list(STL)

基本概念

list是一个链表,将数据进行链式存储,链表是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链表实现的,链表的组成是有一系列结点组成。
结点组成 :一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。

对任意位置进行快速插入和删除元素,容器遍历速度没有数组快,占用空间比数组大。

STL中的链表是一个双向循环链表。由于链表的存储方式不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器。

需要引入头文件#include <list>

构造函数

默认构造

cpp 复制代码
list<int> l;

区间方式构造

cpp 复制代码
list<int> l2(l.begin(),l.end());

拷贝构造

cpp 复制代码
list<int> l3(l);

n个elem构造

cpp 复制代码
list<int> l4(5,1000);

代码示例

cpp 复制代码
void testList01() {
 //创建list容器 默认构造
 list<int> l;
 //添加数据
 l.push_back(10);
 l.push_back(20);
 l.push_back(30);
 l.push_back(40);

 printList(l);// 10 20 30 40

 //区间方式构造
 list<int> l2(l.begin(),l.end());
 printList(l2);// 10 20 30 40

 //拷贝构造
 list<int> l3(l);
 printList(l3);// 10 20 30 40

 //n个elem
 list<int> l4(5,1000);
 printList(l4);//1000 1000 1000 1000 1000
}

赋值和交换

  1. assign();
  2. swap(lst);
  3. operator=;

代码示例

cpp 复制代码
void testList02() {
 list<int> l;
 l.push_back(10);
 l.push_back(20);
 l.push_back(30);
 l.push_back(40);
 printList(l);// 10 20 30 40

 list<int> l2;
 l2 = l;
 printList(l2);// 10 20 30 40

 list<int> l3;
 l3.assign(l.begin(),l.end());
 printList(l3);// 10 20 30 40

 list<int> l4;
 l4.assign(5,100);
 printList(l4); //100 100 100 100 100

 list<int> l5;
 l5.push_back(2);
 l5.push_back(3);
 l5.push_back(4);
 l5.push_back(5);
 printList(l5); // 2 3 4 5
 //交换
 l.swap(l5);
 printList(l5); // 10 20 30 40
}

大小操作

  1. size();
  2. resize();
  3. empty();

代码示例

cpp 复制代码
void testList03() {

 list<int> l;
 l.push_back(10);
 l.push_back(20);
 l.push_back(30);
 l.push_back(40);
 printList(l);

 if (l.empty()) {
  cout << "list为空"<<endl;
 }
 else {
  cout << "list大小" << l.size()<<endl;//4
 }

 l.resize(10);
 printList(l);//10 20 30 40 0 0 0 0 0 0
 l.resize(11,6);
 printList(l);//10 20 30 40 0 0 0 0 0 0 6

}

插入和删除

  1. push_back():尾部插入元素;
  2. pop_back():删除最后一个元素;
  3. push_front():头部插入元素;
  4. pop_front():删除第一个元素;
  5. insert():插入;
  6. clear():清空;
  7. remove():删除;
  8. erase():删除;

代码示例

cpp 复制代码
void testList04() {

 list<int> l;
 //尾插
 l.push_back(10);
 l.push_back(20);
 l.push_back(30);
 l.push_back(40);

 //头插
 l.push_front(100);
 l.push_front(200);
 l.push_front(300);
 printList(l); //300 200 100 10 20 30 40

 //删除第一个
 l.pop_front();
 printList(l);//200 100 10 20 30 40
 //删除最后一个
 l.pop_back();
 printList(l);//200 100 10 20 30

 //insert插入
 list<int>::iterator it = l.begin();
 it++;
 l.insert(it,1000);
 printList(l); //200 1000 100 10 20 30

 it = l.begin();
 //删除
 l.erase(it);
 printList(l);//1000 100 10 20 30

 //移除容器中所有与该值匹配的元素
 l.remove(1000);
 printList(l);//100 10 20 30

 //清空
 l.clear();
}

数据存取

  1. front():返回第一个元素;
  2. back():返回最后一个元素;
  3. list不可以使用at()方法来获取数据 ,也不可以使用\[\];

代码示例

cpp 复制代码
void testList05() {


 list<int> l;
 //尾插
 l.push_back(10);
 l.push_back(20);
 l.push_back(30);
 l.push_back(40);

 cout<<"第一个元素"<<l.front()<<endl;//10
 cout << "最后一个元素" << l.back() << endl; //40
}

反转和排序

  1. reverse():反转链表;
  2. sort():排序,默认升序;

代码示例

cpp 复制代码
bool myCompare(int v1, int v2) {
 //降序
 return v1 > v2;
}

void testList06() {

 list<int> l;
 //尾插
 l.push_back(10);
 l.push_back(20);
 l.push_back(30);
 l.push_back(40);
 printList(l);//10 20 30 40

 l.reverse();
 printList(l);//40 30 20 10

 l.push_back(66);
 l.push_back(4);
 printList(l);//40 30 20 10 66 4
//默认升序 
l.sort();
 printList(l);//4 10 20 30 40 66
 //降序
 l.sort(myCompare);
 printList(l);//66 40 30 20 10 4

}
相关推荐
布莱克6059 分钟前
Swift 下标(Subscript)详解:从语法到实战
服务器·开发语言·ios·ssh·swift·下标
鲸渔1 小时前
【AI Agent系列课程23-Python 基础23】测试入门:为函数写第一个 pytest 用例
开发语言·python·pytest·ai编程
星恒随风1 小时前
从0实现C++ AI大模型接入SDK(三):环境搭建与ChatSDK快速上手——实现我们的第一个AI聊天程序
linux·开发语言·c++·人工智能·笔记
风落青芜1 小时前
排序算法(二):选择排序、堆排序与冒泡排序
c语言·开发语言·数据结构·算法·排序算法
h_a_o777oah2 小时前
【数据结构】FHQ Treap:无旋转的平衡 BST
数据结构·c++·算法·acm·平衡树·treap·fhq treap
fengkai45452 小时前
Python运维实战
运维·开发语言·python·ssh·sftp·paramiko
撸码猿2 小时前
Ladybird 进入 alpha:从零写的浏览器引擎现在能下载了
c++·rust·开源·浏览器·ladybird
ShineWinsu4 小时前
对于Qt:重写事件步骤的解析
开发语言·c++·qt·客户端·事件·鼠标事件·步骤
2501_930707784 小时前
使用C#从 PowerPoint 演示文稿中提取图片
开发语言·c#·powerpoint
zhanghaha13144 小时前
C# 系列教程_15 类 Class 保姆级系统教程(面向新手,大量示例 + 底层机理)
java·开发语言·c#