【嵌入式——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

}
相关推荐
机器视觉的发动机2 小时前
AI算力中心的能耗挑战与未来破局之路
开发语言·人工智能·自动化·视觉检测·机器视觉
HyperAI超神经2 小时前
在线教程|DeepSeek-OCR 2公式/表格解析同步改善,以低视觉token成本实现近4%的性能跃迁
开发语言·人工智能·深度学习·神经网络·机器学习·ocr·创业创新
R_.L2 小时前
【QT】常用控件(按钮类控件、显示类控件、输入类控件、多元素控件、容器类控件、布局管理器)
开发语言·qt
Zach_yuan2 小时前
自定义协议:实现网络计算器
linux·服务器·开发语言·网络
我在人间贩卖青春2 小时前
C++之this指针
c++·this
云姜.2 小时前
java多态
java·开发语言·c++
CoderCodingNo3 小时前
【GESP】C++五级练习题 luogu-P1865 A % B Problem
开发语言·c++·算法
陳10303 小时前
C++:红黑树
开发语言·c++
一切尽在,你来3 小时前
C++ 零基础教程 - 第 6 讲 常用运算符教程
开发语言·c++
泉-java3 小时前
第56条:为所有导出的API元素编写文档注释 《Effective Java》
java·开发语言