当前位置: 首页 > news >正文

STL---list

目录

1. list的介绍及使用

1.1 list的介绍

1.2 list的使用注意事项

2.list接口介绍及模拟实现

2.1构造​编辑

2.2容量

2.3修改

3.list迭代器

4.迭代器失效

5.模拟实现

6.vector和list的区别


1. list的介绍及使用

1.1 list的介绍

list的文档介绍

1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。

2. list的底层是带头双向循环链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。

3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。

4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。

5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)

1.2 list的使用注意事项

1.list不支持随机访问,不能使用[ ]来访问元素。

2.list.sort()使用的是归并排序,默认为升序,如果需要排降序

// 降序
list<int> lt;
greater<int> it;
lt.sort(it);//匿名对象
lt.sort(greater<int> ());

但是使用list的sort排序效率不是很高,在处理数据量较多的情况下,可以将数据拷贝到vector中,再使用vector.sort(),再将数据拷贝回去。

list<int> lt;vector<int> v(lt.begin(), lt.end());
sort(v.begin(), v.end());
lt.assign(v.begin(), v.end());

2.list接口介绍及模拟实现

2.1构造

void empty_init()
{head = new Node;head->next = head;head->prev = head;_size = 0;
}list()
{empty_init();
}list(const list& lt)
{empty_init();for (auto e : lt){push_back(e);}
}

2.2容量

bool empty()
{return head->next == head;
}size_t size()
{return _size;
}

2.3修改

void push_front(const T& value)
{insert(begin(), value);
}void push_back(const T& value)
{insert(end(), value);
}void pop_front()
{erase(begin());
}void pop_back()
{erase(--end());
}void insert(iterator pos, const T& value = T())
{Node* cur = pos._node;Node* prev = cur->prev;Node* newnode = new Node(value);prev->next = newnode;newnode->prev = prev;newnode->next = cur;cur->prev = newnode;++_size;
}iterator erase(iterator pos)
{Node* cur = pos._node;Node* prev = cur->prev;Node* next = cur->next;prev->next = next;next->prev = prev;delete cur;--_size;return next;
}void swap(list<T>& lt)
{std::swap(lt.head);std::swap(lt.size);
}void clear()
{iterator cur = begin();while (cur != end()){cur = erase(cur);}
}

3.list迭代器

与vector和string不同,list的迭代器需要自己实现,list迭代器可以理解为一个指针,指向list中的某个结点,而链表的每个结点在物理空间上并不连续,所以当迭代器++的时候,并不能直接到下一个结点的位置,并且*的时候,并不知道访问的是链表中的哪个数据,所以我们需要自己实现,将迭代器进行封装。

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->date;}Ptr operator->(){return &_node->date;}self& operator++(){_node = _node->next;return *this;}self& operator--(){_node = _node->prev;return *this;}bool operator!=(const self& lt){return _node != lt._node;}
};

需要注意的是,类模板参数有三个,是为了同时能实现迭代器和const迭代器,在list类中直接传不同的参数即可。

4.迭代器失效

此处大家可将迭代器暂时理解成类似于指针,迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。

void TestListIterator1()
{int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };list<int> l(array, array+sizeof(array)/sizeof(array[0]));auto it = l.begin();while (it != l.end()){// erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给其赋值l.erase(it);++it;}
}//修改void TestListIterator()
{int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };list<int> l(array, array+sizeof(array)/sizeof(array[0]));auto it = l.begin();while (it != l.end()){it = l.erase(it);}
}

5.模拟实现

namespace cola
{template<class T>struct _list_node{T date;_list_node<T>* next;_list_node<T>* prev;_list_node(const T& value = T()): date(value), next(nullptr), prev(nullptr){}};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->date;}Ptr operator->(){return &_node->date;}self& operator++(){_node = _node->next;return *this;}self& operator--(){	_node = _node->prev;return *this;}self operator++(int){self temp(*this);_node = _node->next;return temp;}self operator--(int){self temp(*this);_node = _node->prev;return temp;}bool operator!=(const self& lt){return _node != lt._node;}bool operator==(const self& lt){return _node == lt._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 head->next;}iterator end(){return head;}const_iterator begin() const{return head->next;}const_iterator end() const{return head;}void empty_init(){head = new Node;head->next = head;head->prev = head;_size = 0;}list(){empty_init();}~list(){clear();delete head;}list(const list& lt){empty_init();for (auto e : lt){push_back(e);}}list& operator=(const list& lt){if (head != lt.head){clear();for (auto e : lt){push_back(e);}}return *this;}void push_front(const T& value){insert(begin(), value);}void push_back(const T& value){insert(end(), value);}void pop_front(){erase(begin());}void pop_back(){erase(--end());}void insert(iterator pos, const T& value = T()){Node* cur = pos._node;Node* prev = cur->prev;Node* newnode = new Node(value);prev->next = newnode;newnode->prev = prev;newnode->next = cur;cur->prev = newnode;++_size;}iterator erase(iterator pos){Node* cur = pos._node;Node* prev = cur->prev;Node* next = cur->next;prev->next = next;next->prev = prev;delete cur;--_size;return next;}void swap(list<T>& lt){std::swap(lt.head);std::swap(lt.size);}void clear(){iterator cur = begin();while (cur != end()){cur = erase(cur);}}bool empty(){return head->next == head;}size_t size(){return _size;}private:Node* head;size_t _size;};//打印函数(适用于任何容器)template<typename Container>void print_container(const Container& lt){typename Container::const_iterator it = lt.begin();while (it != lt.end()){cout << *it << " ";++it;}cout << endl;}
}

6.vector和list的区别

http://www.lryc.cn/news/135764.html

相关文章:

  • python判断ip所属地区 python 判断ip 网段
  • 大数据分析案例-基于LightGBM算法构建糖尿病确诊预测模型
  • Mysql查询重复数据常用方法
  • Go framework-GORM
  • FirmAE 工具安装(解决克隆失败 网络问题解决)
  • css实现九宫格布局
  • linux下系统问题排查基本套路
  • 想解锁禁用的iPhone?除了可以使用电脑之外,这里还有不需要电脑的方法!
  • 基于Springboot+Thymeleaf学生在线考试管理系统——LW模板
  • STM32f103c6t6/STM32f103c8t6寄存器开发
  • MySQL Connection not available.
  • PHP反序列化 字符串逃逸
  • DockerFile解析
  • 斯坦福大学医学院教授:几年内ChatGPT之类的AI将纳入日常医学实践
  • golang 命令行 command line (flag,os,arg,args)
  • Shell语法揭秘:深入探讨常见Linux Shell之间的语法转换
  • Python3 基础语法
  • spring boot分装通用的查询+分页接口
  • 【OpenCV】OpenCV环境搭建,Mac系统,C++开发环境
  • node安装node-sass依赖失败(版本不一致)
  • 联想小新Pro 16笔记本键盘失灵处理方法
  • python 连接Redis 数据库
  • 使用 wxPython 和 pymupdf进行 PDF 加密
  • Mysql性能优化:什么是索引下推?
  • Pytorch建立MyDataLoader过程详解
  • 十问华为云 Toolkit:开发插件如何提升云上开发效能
  • NO.06 自定义映射resultMap
  • 国产精品:讯飞星火最新大模型V2.0
  • 网络综合布线实训室方案(2023版)
  • Qt应用开发(基础篇)——文本编辑窗口 QTextEdit