news 2026/1/19 0:14:38

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/1/6 4:59:12

企业级应用中的模块加载错误实战解决方案

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 开发一个模块加载错误诊断沙盒环境&#xff0c;模拟企业级应用中可能遇到的各种模块加载问题场景&#xff0c;包括&#xff1a;1)不同服务器配置下的模块加载行为&#xff1b;2)各…

作者头像 李华
网站建设 2026/1/17 3:50:42

传统VS现代:Python下载工具开发效率对比实验

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 分别用传统方式和AI辅助方式实现同一个FTP文件下载工具。传统方式要求完整手写代码&#xff1b;AI方式只需描述需求&#xff1a;需要支持FTP协议、多文件队列下载、传输速率显示和…

作者头像 李华
网站建设 2026/1/17 18:08:38

VS2022实战应用案例分享

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个VS2022实战项目&#xff0c;包含完整的功能实现和部署方案。点击项目生成按钮&#xff0c;等待项目生成完整后预览效果 VS2022实战应用案例分享&#xff1a;从开发到部署的…

作者头像 李华
网站建设 2026/1/17 22:52:53

编程小白也能做:用快马创建你的第一个记事本应用

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个最简单的记事本Web应用&#xff0c;适合编程新手学习&#xff0c;功能包括&#xff1a;1. 纯文本输入区域 2. 保存/打开本地文件功能 3. 基本的字体样式设置(加粗/斜体) 4…

作者头像 李华
网站建设 2026/1/9 14:23:50

VibeVoice-WEB-UI是否支持语音生成任务提醒?待办事项

VibeVoice-WEB-UI&#xff1a;当TTS不再只是“朗读”&#xff0c;而是“演绎” 在播客制作人熬夜剪辑音频、有声书团队反复校对角色语气的今天&#xff0c;我们是否还能想象一种可能——只需输入一段结构化文本&#xff0c;系统就能自动理解谁该说什么、何时停顿、以何种情绪表…

作者头像 李华
网站建设 2026/1/17 18:54:14

零基础教程:用AI制作你的第一个Chrome插件

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 生成一个最简单的Chrome插件入门示例&#xff1a;当用户点击插件图标时&#xff0c;弹出窗口显示当前网页标题和URL。包含完整的manifest.json配置&#xff0c;使用最基础的browse…

作者头像 李华