当前位置:网站首页>45.【list链表的应用】
45.【list链表的应用】
2022-07-30 23:32:00 【李在奋斗……】
【头部插入一个数据】
函数名.push_front()
【尾部插入一个数据】
函数名.push_back()
【多元插入数据】
函数名.insert(函数名.begin(),n) 从哪开始 插入谁
【多元插入数据2】
函数名.insert(函数名.begin(),n,m) 从哪开始 插入几个 插入谁
【利用迭代器插入】
iist〈函数类型〉 ::iterator 对象名;
对象名=函数名.begin();
函数名.insert(对象名,个数,元素)
【删除头一个元素】
函数名.pop_front()
【删除最后一个元素】
函数名.pop_back()
【多元删除数据】
函数名1.erase(函数名1.begin(),函数名1.end()
【链表遍历只能是迭代器遍历】
iist〈函数类型〉 ::iterator 对象名;
for(对象名=函数名.begin();对象名!=end();对象名++)
{
cout<<*对象名;
}
#include <list>
#include <iostream>
using namespace std;
int main()
{
list<int> list_a; //初始化为0
int s[4] = { 2,5,8 };
list<int> list_b(s, s + sizeof(s)/sizeof(int)); //普通数组复制给链表
list_b.push_front(1); //在头部插入
list_b.push_back(2); //在尾部插入
list_b.insert(list_b.begin(), 66); // 从哪插入, 插入什么
list_b.insert(list_b.end(), 88);
list_b.insert(list_b.end(),3, 100); // 从哪插入, 插入几个,插入谁
list<int>::iterator it; //利用迭代器锦新插入
it = list_b.begin();
it++;
list_b.insert(it, 5, 111);
list_b.insert(list_b.begin(), s, s + sizeof(s) / sizeof(int)); //插入普通数组
list_b.pop_front(); //删除头部一个
list_b.pop_back(); //删除尾部一个
//list_b.erase(list_b.begin(), list_b.end()); // 从头到位删除
list<int>::iterator itor;
for (itor = list_b.begin(); itor!= list_b.end(); itor++)
{
cout << *itor << " ";
}
return 0;
}


边栏推荐
猜你喜欢

mysql中关于存储过程无法实现迁移复制表中数据问题

微软商店出现【0x800706D9】解决方法

【LeetCode】70. 爬楼梯 - Go 语言题解

$\text{ARC 145}$

测试人面试 常被问到的计算机网络题,高薪回答模板来了

Gxlcms audio novel system/novel listening system source code

机器学习1一回归模型(二)

uniapp folding box secondary loop

A detailed explanation: SRv6 Policy model, calculation and drainage

Reverse linked list - in-place inversion method
随机推荐
ZZULIOJ:1119: sequence order
# # yyds dry goods inventory interview will brush TOP101: to determine whether there is a part of the list
如何在 AWS 中应用 DevOps 方法?
Week 19 Progress (Understanding IoT Basics)
二进制序列
Gxlcms audio novel system/novel listening system source code
2021GDCPC Guangdong University Student Programming Competition B.Byfibonacci
grub learning
Go语学习笔记 - gorm使用 - gorm处理错误 Web框架Gin(十)
C# VSCode & Rider引用命名空间快捷键
“蔚来杯“2022牛客暑期多校训练营4 DHKLN
通过对抗性知识蒸馏压缩深度图神经网络
Summary of BFS questions
Detailed operator
ZZULIOJ: 1120: the most value to exchange
Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
pytorch的安装注意事项
[0x800706D9] solution appears in Microsoft Store
"NIO Cup" 2022 Nioke Summer Multi-School Training Camp 4 DHKLN
Chevrolet Trailblazer, the first choice for safety and warmth for your family travel