news 2026/7/19 15:18:17

list 的使用及模拟实现

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
list 的使用及模拟实现

list 使用:

1.list 的构造:


2.list iterator的使用:


3.list capacity:


4.list element access:


5.list modifiers:


list 模拟实现:

#pragma once #include<assert.h> namespace yx { template<class T> struct list_node { list_node<T>* _next; list_node<T>* _prev; T _val; list_node(const T& val = T()) //'=T()'类似'int()'表示默认参数 :_next(nullptr) , _prev(nullptr) , _val(val) {} }; template<class T, class Ref, class Ptr> //主要用于实现普通迭代器与常量迭代器 struct __list_iterator { typedef list_node<T> Node; typedef __list_iterator<T, Ref, Ptr> self; Node* _node; __list_iterator(Node* node) :_node(node) { } Ref operator*() { return _node->_val; } Ptr operator->() { return &_node->_val; } self& operator++() { _node = _node->_next; return *this; } self operator++(int) { self tmp(*this); _node = _node->_next; return tmp; } self& operator--() { _node = _node->_prev; return *this; } self operator--(int) { self tmp(*this); _node = _node->_prev; return tmp; } bool operator==(const self& it)const { return _node == it._node; } bool operator!=(const self& it)const { return _node != it._node; } }; template<class T> class list { typedef list_node<T> Node; public: typedef __list_iterator<T, T&, T*> iterator; typedef __list_iterator<T, const T&, const T*> const_iterator; iterator begin() { return iterator(_head->_next); } iterator end() { return _head; } const_iterator begin() const { return const_iterator(_head->_next); } const_iterator end() const { return const_iterator(_head); } void empty_init() { _head = new Node; _head->_prev = _head; _head->_next = _head; _size = 0; } list() { empty_init(); } list(const list<T>& lt) { empty_init(); for (auto& e : lt) { push_back(e); } } void swap(list<T>& lt) { std::swap(_head, lt._head); std::swap(_size, lt._size); } list<T>& operator=(list<T> lt) { swap(lt); return *this; } ~list() { clear(); delete _head; _head = nullptr; } void clear() { iterator it = begin(); while (it != end()) { it = erase(it); } _size = 0; } void push_back(const T& x) { insert(end(), x); } void push_front(const T& x) { insert(begin(), x); } void pop_back() { erase(--end()); } void pop_front() { erase(begin()); } iterator insert(iterator pos, const T& x) { Node* cur = pos._node; Node* prev = cur->_prev; Node* newnode = new Node(x); prev->_next = newnode; newnode->_next = cur; cur->_prev = newnode; newnode->_prev = prev; ++_size; return newnode; } iterator erase(iterator pos) { assert(pos != end()); Node* cur = pos._node; Node* prev = cur->_prev; Node* next = cur->_next; prev->_next = next; next->_prev = prev; delete cur; --_size; return next; } size_t size() { return _size; } private: Node* _head; size_t _size; }; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/19 15:18:02

3分钟搞定黑苹果EFI配置:OpCore Simplify让复杂配置变简单

3分钟搞定黑苹果EFI配置&#xff1a;OpCore Simplify让复杂配置变简单 【免费下载链接】OpCore-Simplify A tool designed to simplify the creation of OpenCore EFI 项目地址: https://gitcode.com/GitHub_Trending/op/OpCore-Simplify 你是否曾经为黑苹果的EFI配置感…

作者头像 李华
网站建设 2026/7/19 15:17:49

3分钟掌握网易云音乐NCM解密:免费工具ncmdump完全指南

3分钟掌握网易云音乐NCM解密&#xff1a;免费工具ncmdump完全指南 【免费下载链接】ncmdump 项目地址: https://gitcode.com/gh_mirrors/ncmd/ncmdump 你是否曾经在网易云音乐下载了心爱的歌曲&#xff0c;却发现只能在特定客户端播放&#xff1f;NCM加密格式就像一把无…

作者头像 李华
网站建设 2026/7/19 15:12:21

如何快速上手MOSS-Music-8B-Thinking-6bit:5分钟完成音乐AI分析部署

如何快速上手MOSS-Music-8B-Thinking-6bit&#xff1a;5分钟完成音乐AI分析部署 【免费下载链接】MOSS-Music-8B-Thinking-6bit 项目地址: https://ai.gitcode.com/hf_mirrors/mlx-community/MOSS-Music-8B-Thinking-6bit MOSS-Music-8B-Thinking-6bit是一款基于MLX框架…

作者头像 李华
网站建设 2026/7/19 15:11:41

现代C:极致优化(下):如何实现高性能的 C 程序?

引用在上一讲中&#xff0c;我介绍了几个用于编写高性能 C 代码的实用技巧。今天&#xff0c;我们继续聊这个话题&#xff0c;来讨论其他几种常见的 C 代码和程序优化技巧&#xff0c;它们分别是利用循环展开、使用条件传送指令、尾递归调用优化&#xff0c;以及为编译器指定更…

作者头像 李华
网站建设 2026/7/19 15:10:05

Wand-Enhancer实战手册:三步解锁WeMod完整功能

Wand-Enhancer实战手册&#xff1a;三步解锁WeMod完整功能 【免费下载链接】Wand-Enhancer Advanced UX and interoperability extension for Wand (WeMod) app 项目地址: https://gitcode.com/GitHub_Trending/we/Wand-Enhancer 还在为WeMod专业版的付费墙而烦恼吗&…

作者头像 李华