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在进行插入操作时,不需要移动数据,因此进行元素插入不会导致迭代器失效,但在进行元素删除时,指向删除元素的迭代器会失效,其他的并没有影响。

相关推荐
路由侠内网穿透5 小时前
本地部署开源持续集成和持续部署系统 Woodpecker CI 并实现外部访问
服务器·网络·windows·ci/cd·开源
CsharpDev-奶豆哥6 小时前
ASP.NET中for和foreach使用指南
windows·microsoft·c#·asp.net·.net
立志成为大牛的小牛8 小时前
数据结构——十七、线索二叉树找前驱与后继(王道408)
数据结构·笔记·学习·程序人生·考研·算法
Algo-hx8 小时前
数据结构入门 (七):从“链接”到“分支” —— 初探树与二叉树
数据结构
小贾要学习9 小时前
【数据结构】C++实现红黑树
数据结构·c++
Microsoft Word10 小时前
跨平台向量库:Linux & Windows 上一条龙部署 PostgreSQL 向量扩展
linux·windows·postgresql
Wx-bishekaifayuan11 小时前
基于微信小程序的社区图书共享平台设计与实现 计算机毕业设计源码44991
javascript·vue.js·windows·mysql·pycharm·tomcat·php
周杰伦_Jay12 小时前
【Java集合体系】全面解析:架构、原理与实战选型
java·开发语言·数据结构·链表·架构
zhuyasen12 小时前
在某些 Windows 版本,Go 1.25.x 编译出来的 exe 运行报错:此应用无法在你的电脑上运行
windows·go·编译器
tkevinjd13 小时前
反转链表及其应用(力扣2130)
数据结构·leetcode·链表