news 2026/2/12 17:52:48

C/C++内存管理_cpp

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C/C++内存管理_cpp

list的底层是双向链表结构,双向链表中,每个元素存储在互不相关的节点中,在节点中保存着指向前一个节点和后一个节点的指针。

2,与forward_list相似,forward_list是单链表,只能向前迭代。而list是双向链表,可以前后迭代。

3,list与vector相比,它的插入和删除元素效率更高,不需要遍历一遍原链表。

二,list的一些基本使用

2.1,list的构造函数

list()//无参构造函数list(size_t n,const value_type& val = value_type())//n个va的构造list(InputIterator first,InputIterator last)//迭代器区间构造list(const list& x)//拷贝构造

示例:

void Test_List1() { list<int> lt1; //无参构造,值为空 list<int> lt2(4, 100); //4个100初始化 list<int> lt3(lt2.begin(), lt2.end()); //迭代器区间构造 list<int> lt4(lt2); //拷贝构造 }

注:C++11提供了initializer_list的新类型,主要用于初始化

用法如下:

initializer_list<int> lt = { 1,2,3,4,5 }; list<int> lst(lt); //用lt中的元素初始化lst

但一般在写的时候,可以使用隐式类型转化

list<int> lt = { 1,2,3,4,5 }; //和上面代码一样

2.2,list的迭代器

list提供了双向迭代器,可用于遍历和操作容器中的数据。

  1. begin():返回指向第一个元素的迭代器。
  2. end():返回指向最后一个元素之后位置的迭代器。
  3. rbegin():返回指向最后一个元素的逆向迭代器。
  4. rend():返回指向第一个元素之前位置的逆向迭代器。

代码语言:javascript

AI代码解释

void Test_List2() { list<int> myList = { 1, 2, 3, 4, 5 }; // 正向迭代器遍历list cout << "正向遍历list: "; list<int>::iterator itr; for (itr = myList.begin(); itr != myList.end(); ++itr) { cout << *itr << " "; } cout << endl; // 逆向迭代器遍历list cout << "逆向遍历list: "; list<int>::reverse_iterator ritr; for (ritr = myList.rbegin(); ritr != myList.rend(); ++ritr) { cout << *ritr << " "; } cout << endl; }

运行结果:

2.3,list的插入

对于插入操作,list实现了如下几个常用的接口

push_back(x) //尾插 push_front(x) //头插 insert(iterator it ,x) //在迭代器位置之前插入x

示例:

代码语言:javascript

AI代码解释

void Test_List3() { list<int> lt; //尾插 lt.push_back(1); lt.push_back(2); cout << "push_back:"; for (auto e : lt) cout << e << " "; cout << endl; //头插 lt.push_front(4); lt.push_front(5); cout << "push_front:"; for (auto e : lt) cout << e << " "; cout << endl; //迭代器 lt.insert(lt.begin(), 9); cout << "insert:"; for (auto e : lt) cout << e << " "; cout << endl; }

运行结果:

2.4,list的删除

对于删除,list提供的接口如下:

pop_back() //尾删 pop_front() //头删 erase(iterator it) //删除it位置 erase(iterator first,iterator last) //删除[first,last)区间

https://www.dongchedi.com/article/7591968509647880728
https://www.dongchedi.com/article/7591967199926239806
https://www.dongchedi.com/article/7591971294128882201
https://www.dongchedi.com/article/7591967894913876504
https://www.dongchedi.com/article/7591967152509534745
https://www.dongchedi.com/article/7591971859542114878
https://www.dongchedi.com/article/7591968062619353624
https://www.dongchedi.com/article/7591967425524941337
https://www.dongchedi.com/article/7591968380505604670
https://www.dongchedi.com/article/7591971665794744857
https://www.dongchedi.com/article/7591968456015364670
https://www.dongchedi.com/article/7591967435696390718
https://www.dongchedi.com/article/7591956921293554238
https://www.dongchedi.com/article/7591967089679123006
https://www.dongchedi.com/article/7591966696349811225
https://www.dongchedi.com/article/7591967652885938712
https://www.dongchedi.com/article/7591966696349975065
https://www.dongchedi.com/article/7591967199926010430
https://www.dongchedi.com/article/7591966696349581849
https://www.dongchedi.com/article/7591966696349712921
https://www.dongchedi.com/article/7591952901548130840
https://www.dongchedi.com/article/7591977358085947929
https://www.dongchedi.com/article/7591957025727447577
https://www.dongchedi.com/article/7591957710099219006
https://www.dongchedi.com/article/7591957234859754009
https://www.dongchedi.com/article/7591958038261891609
https://www.dongchedi.com/article/7591957038725415448
https://www.dongchedi.com/article/7591966916986896920
https://www.dongchedi.com/article/7591957025727218201
https://www.dongchedi.com/article/7591956099243508249
https://www.dongchedi.com/article/7591956099243639321
https://www.dongchedi.com/article/7591957122154480190
https://www.dongchedi.com/article/7591956180667449880
https://www.dongchedi.com/article/7591956187550630425
https://www.dongchedi.com/article/7591952365440696894
https://www.dongchedi.com/article/7591953708586648089
https://www.dongchedi.com/article/7591954611506987545
https://www.dongchedi.com/article/7591955614520050201
https://www.dongchedi.com/article/7591966752641647166
https://www.dongchedi.com/article/7591954109906403865
https://www.dongchedi.com/article/7591955053871743513
https://www.dongchedi.com/article/7591953699526722072
https://www.dongchedi.com/article/7591954892609290814
https://www.dongchedi.com/article/7591954689361822232
https://www.dongchedi.com/article/7591951704036229657
https://www.dongchedi.com/article/7591979961964380734
https://www.dongchedi.com/article/7591972172239782424
https://www.dongchedi.com/article/7591968413292495385
https://www.dongchedi.com/article/7591980885017723416
https://www.dongchedi.com/article/7591978510097744446
https://www.dongchedi.com/article/7591969027937845822
https://www.dongchedi.com/article/7591979709920346649
https://www.dongchedi.com/article/7591980885017690648
https://www.dongchedi.com/article/7591969174093267480
https://www.dongchedi.com/article/7591968108445843993
https://www.dongchedi.com/article/7591965826828829246

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/10 16:54:35

MINIFORGE vs Conda:环境管理效率的全面对比测试

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个性能对比测试套件&#xff0c;比较MINIFORGE和Conda在以下方面的表现&#xff1a;1) 环境创建和删除速度&#xff1b;2) 包安装和更新效率&#xff1b;3) 内存和CPU占用&a…

作者头像 李华
网站建设 2026/2/10 11:41:11

Tushare数据获取:传统开发vsAI辅助效率对比

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个对比实验项目&#xff1a;1. 传统方式手动编写Tushare接口调用代码&#xff08;获取日线数据、数据清洗、存储&#xff09;2. 使用快马AI生成相同功能代码 3. 比较两种方式…

作者头像 李华
网站建设 2026/2/12 5:17:30

电商网站实战:用Flask+AI构建完整商品系统

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 开发一个电商商品管理系统&#xff0c;功能包括&#xff1a;1.多级商品分类 2.商品CRUD及图片上传 3.全文搜索(Elasticsearch集成) 4.用户评价系统 5.支付宝/微信支付接口 6.销售数…

作者头像 李华
网站建设 2026/2/12 14:17:10

全屏程序切换工具,激活选中窗口快速切换

软件介绍 今天给大伙儿安利个小众但特管用的工具&#xff0c;它叫 Windows选择窗口激活器。专门解决那种全屏程序&#xff08;尤其是游戏&#xff09;卡住没法切换的破事儿&#xff0c;关键时刻能救急&#xff01; 使用场景与方法 有时候玩全屏游戏或开全屏程序&#x…

作者头像 李华
网站建设 2026/2/11 10:25:47

VibeVoice-WEB-UI是否支持语音生成自动重试?容错机制

VibeVoice-WEB-UI的容错能力&#xff1a;语音生成中断后如何恢复&#xff1f; 在AI音频内容爆发式增长的今天&#xff0c;播客、有声书、虚拟访谈等长时语音应用对合成系统的稳定性提出了前所未有的挑战。一个理想的TTS系统不仅要“能说话”&#xff0c;更要“说得久、说得好、…

作者头像 李华
网站建设 2026/2/5 16:38:34

C#基础语言--Windows Form基础:菜单控件、下拉列表控件

01 菜单控件Form界面可以在请在此处键入那里填写想要填写的东西下拉列表控件可以通过属性列表里的Items集合添加下拉列表的数据也可以通过代码添加数据首先通过SelectedIndex 设置默认选择的项comboBox1.SelectedIndex 0;通过代码来添加数据源List<string>list new Lis…

作者头像 李华