list基本使用

list基本使用

list容器底层是带头双向链表结构,可以在常数范围内在任意位置进行输入和删除,但不支持任意位置的随机访问(如不支持 下标访问),下面介绍list容器的基本使用接口。

template < class T, class Alloc = allocator > class list;

构造

1.无参构造

list()

2.使用n个元素val进行构造

list(size_type n,const value_type& value = value_type());

3,拷贝构造

list(const list& x);

4.使用迭代器区间进行构造

list(InputIterator first, InputIterator last);

迭代器

1.正向迭代器

iterator begin();

const_iterator begin() const;

terator end();

const_iterator end() const;

2.反向迭代器

reverse_iterator rbegin();

const_reverse_iterator rbegin() const;

reverse_iterator rend();

const_reverse_iterator rend() const;

容量

1.判断链表是否为空

bool empty() const;

2.获取链表中有效节点个数

size_type size() const;

3.修改容量大小

void resize (size_type n, value_type val = value_type());

访问

1.获取链表第一个节点的值的引用

reference front();

const_reference front() const;

2.获取链表最后一个节点的值的引用

reference back();

const_reference back() const;

修改

1.元素首插

void push_front (const value_type& val);

2.元素尾插

void push_back (const value_type& val);

3.在position位置插入值为val的元素

iterator insert (iterator position, const value_type& val);

void insert (iterator position, size_type n, const value_type& val);

template

void insert (iterator position, InputIterator first, InputIterator last);

//

//Notice that the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.

5.元素首删

void pop_front();

6.元素尾删

void pop_back();

7.删除position位置的元素

iterator erase (iterator position);

iterator erase (iterator first, iterator last);

//

//the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.

8.清空链表有效元素

void clear();

9.交换两个链表的所有元素

void swap (list& x);

需要注意的是,list在进行插入操作时,不需要移动数据,因此进行元素插入不会导致迭代器失效,但在进行元素删除时,指向删除元素的迭代器会失效,其他的并没有影响。

相关推荐
泡海椒4 小时前
JQuick-SQL 快速开始:注册内存表并执行查询
数据库·windows·sql
钱栈up6 小时前
耳机音效不对,我顺手查了系统更新,结果挖出个崩溃了整天的服务
windows·shell
xiao77mwq7 小时前
Wireshark 在 Windows 上没法按进程抓包,我补了一条路
windows
Helix2507 小时前
MDT自动化部署实战:构建企业级Windows批量装机与PXE网络引导环境
windows·mdt自动化部署·wds网络启动·pxe引导·windows批量装机·任务序列·无人值守安装
学IT的周星星8 小时前
记忆系统:让 Agent 记住你是谁、上次聊到哪
运维·服务器·人工智能·windows·大模型·llm·agent
CS创新实验室10 小时前
一棵树怎么做到“自平衡还不太费力“?——红黑树的旋转与染色
数据结构·算法·红黑树·408·计算机考研
撞强10 小时前
Windows 上从零开始:Obsidian + Copilot(Icompify)+ 客户洞察实战 · 图文指南
windows·copilot·obsidian·icompify
Logic10110 小时前
C语言/数据结构位运算题解:异或XOR找出被挑选一次的果子编号——只出现一次的数字
c语言·数据结构·算法·数组·位运算·时间复杂度·空间复杂度
weixin_5716674111 小时前
Windows 通过 SSH 反向隧道向 Ubuntu 转发代理
linux·运维·windows·代理
事圆则缓11 小时前
Kotlin 集合与序列:给页面一份清楚的数据
开发语言·windows·kotlin