外贸建网站哪家好,什么是网络营销的技术,无锡网页设计培训班,网页版聊天软件目录 1.list的介绍 2.list的使用
2.1list的构造
2.2list iterator的使用
2.3list capacity 2.4list element access 2.5list modifers
2.6list的迭代器失效
3.list的模拟实现
4.list与vector的对比 欢迎 1.list的介绍
list的文档介绍
cplusplus.com/reference/list/li…目录 1.list的介绍 2.list的使用
2.1list的构造
2.2list iterator的使用
2.3list capacity 2.4list element access 2.5list modifers
2.6list的迭代器失效
3.list的模拟实现
4.list与vector的对比 欢迎 1.list的介绍
list的文档介绍
cplusplus.com/reference/list/list/?kwlist 2.list的使用
2.1list的构造 list (size_type n, const value_type val value_type()) 构造的 list 中包含 n 个值为 val 的 元素 list() 构造空的 list list(const list x) 拷贝构造函数 list (InputIterator first, InputIterator last) 用 [first, last) 区间中的元素构造 list
list构造的使用
void TestList1()
{listint l1; // 构造空的l1listint l2(4, 100); // l2中放4个值为100的元素listint l3(l2.begin(), l2.end()); // 用l2的[begin(), end()左闭右开的区间构造l3listint l4(l3); // 用l3拷贝构造l4// 以数组为迭代器区间构造l5int array[] { 16,2,77,29 };listint l5(array, array sizeof(array) / sizeof(int));// 列表格式初始化C11listint l6{ 1,2,3,4,5 };// 用迭代器方式打印l5中的元素listint::iterator it l5.begin();while (it ! l5.end()){cout *it ;it;} cout endl;// C11范围for的方式遍历for (auto e : l5)cout e ;cout endl;
}
2.2list iterator的使用
beginend返回第一个元素的迭代器返回最后一个元素下一个位置的迭代器rbeginrend返回死一个元素的reverse_iterator,即end位置返回最后一个元素下一个位置的reverse_iterator即begin位置 【注意】 1. begin 与 end 为正向迭代器对迭代器执行 操作迭代器向后移动 2. rbegin(end) 与 rend(begin) 为反向迭代器对迭代器执行 操作迭代器向前移动 // 注意遍历链表只能用迭代器和范围for
void PrintList(const listint l)
{// 注意这里调用的是list的 begin() const返回list的const_iterator对象for (listint::const_iterator it l.begin(); it ! l.end(); it){cout *it ;// *it 10; 编译不通过}cout endl;
}void TestList2()
{int array[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };listint l(array, array sizeof(array) / sizeof(array[0]));// 使用正向迭代器正向list中的元素auto it l.begin(); while (it ! l.end()){cout *it ;it;}cout endl;// 使用反向迭代器逆向打印list中的元素// listint::reverse_iterator rit l.rbegin();auto rit l.rbegin();while (rit ! l.rend()){cout *rit ;rit;}cout endl;
}
2.3list capacity
empty检测list是否为空是返回true否则返回falsesize返回list中有效节点的个数 2.4list element access
front返回list的第一个节点中值的引用back返回list的最后一个节点值的引用 2.5list modifers push_front在list首元素前插入值为val的元素pop_front删除list中第一个元素push_back在list尾部插入值为val的元素pop_back删除list中最后一个元素insert在list position位置中插入中为val的元素erase删除list position位置的元素clear清空list中的有效元素 list的插入和删除使用代码演示 void TestList3()
{int array[] { 1, 2, 3 };listint L(array, array sizeof(array) / sizeof(array[0]));// 在list的尾部插入4头部插入0L.push_back(4);L.push_front(0);PrintList(L);// 删除list尾部节点和头部节点L.pop_back();L.pop_front();PrintList(L);
}// insert /erase
void TestList4()
{int array1[] { 1, 2, 3 };listint L(array1, array1 sizeof(array1) / sizeof(array1[0]));// 获取链表中第二个节点auto pos L.begin();cout *pos endl;// 在pos前插入值为4的元素L.insert(pos, 4);PrintList(L);// 在pos前插入5个值为5的元素L.insert(pos, 5, 5);PrintList(L);// 在pos前插入[v.begin(), v.end)区间中的元素vectorint v{ 7, 8, 9 };L.insert(pos, v.begin(), v.end());PrintList(L);// 删除pos位置上的元素L.erase(pos);PrintList(L);// 删除list中[begin, end)区间中的元素即删除list中的所有元素L.erase(L.begin(), L.end());PrintList(L);
}// resize/swap/clear
void TestList5()
{// 用数组来构造listint array1[] { 1, 2, 3 };listint l1(array1, array1 sizeof(array1) / sizeof(array1[0]));PrintList(l1);// 交换l1和l2中的元素listint l2;l1.swap(l2);PrintList(l1);PrintList(l2);// 将l2中的元素清空l2.clear();cout l2.size() endl;
} 2.6list的迭代器失效 迭代器失效即迭代器所指向的节点的无 效即该节点被删除了 。因为 list 的底层结构为带头结点的双向循环链表 因此 在 list 中进行插入 时是不会导致 list 的迭代器失效的只有在删除时才会失效并且失效的只是指向被删除节点的迭 代器其他迭代器不会受到影响 。 #include list
using namespace std;void TestListIterator1()
{int array[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };// 初始化链表l使用array数组中的元素listint l(array, array sizeof(array) / sizeof(array[0]));auto it l.begin(); // 创建一个迭代器it指向链表的开头// 遍历链表while (it ! l.end()){// 删除当前it指向的节点// 这里it已经失效不能再直接使用l.erase(it);it; // 增加it指向下一个节点但此时it可能已经无效}
}// 改正后的版本
void TestListIterator()
{int array[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };// 初始化链表l使用array数组中的元素listint l(array, array sizeof(array) / sizeof(array[0]));auto it l.begin(); // 创建一个迭代器it指向链表的开头// 遍历链表while (it ! l.end()){// 删除当前it指向的节点并返回指向下一个节点的迭代器// it会被更新为l.erase(it)的返回值即指向下一个有效节点it l.erase(it); // 先删除然后更新it}
}3.list的模拟实现
#pragma once#include iostream
using namespace std;
#include assert.hnamespace pzn
{// List的节点类templateclass Tstruct ListNode{ListNode(const T val T()): _prev(nullptr), _next(nullptr), _val(val){}ListNodeT* _prev;ListNodeT* _next;T _val;};/*List 的迭代器迭代器有两种实现方式具体应根据容器底层数据结构实现1. 原生态指针比如vector2. 将原生态指针进行封装因迭代器使用形式与指针完全相同因此在自定义的类中必须实现以下方法1. 指针可以解引用迭代器的类中必须重载operator*()2. 指针可以通过-访问其所指空间成员迭代器类中必须重载oprator-()3. 指针可以向后移动迭代器类中必须重载operator()与operator(int)至于operator--()/operator--(int)释放需要重载根据具体的结构来抉择双向链表可以向前 移动所以需要重载如果是forward_list就不需要重载--4. 迭代器需要进行是否相等的比较因此还需要重载operator()与operator!()*/templateclass T, class Ref, class Ptrclass ListIterator{typedef ListNodeT Node;typedef ListIteratorT, Ref, Ptr Self;// Ref 和 Ptr 类型需要重定义下实现反向迭代器时需要用到public:typedef Ref Ref;typedef Ptr Ptr;public://// 构造ListIterator(Node* node nullptr): _node(node){}//// 具有指针类似行为Ref operator*() { return _node-_val;}Ptr operator-() { return (operator*()); }//// 迭代器支持移动Self operator(){_node _node-_next;return *this;}Self operator(int){Self temp(*this);_node _node-_next;return temp;}Self operator--(){_node _node-_prev;return *this;}Self operator--(int){Self temp(*this);_node _node-_prev;return temp;}//// 迭代器支持比较bool operator!(const Self l)const{ return _node ! l._node;}bool operator(const Self l)const{ return _node ! l._node;}Node* _node;};templateclass Iteratorclass ReverseListIterator{// 注意此处typename的作用是明确告诉编译器Ref是Iterator类中的一个类型而不是静态成员变量// 否则编译器编译时就不知道Ref是Iterator中的类型还是静态成员变量// 因为静态成员变量也是按照 类名::静态成员变量名 的方式访问的public:typedef typename Iterator::Ref Ref;typedef typename Iterator::Ptr Ptr;typedef ReverseListIteratorIterator Self;public://// 构造ReverseListIterator(Iterator it): _it(it){}//// 具有指针类似行为Ref operator*(){Iterator temp(_it);--temp;return *temp;}Ptr operator-(){return (operator*());}//// 迭代器支持移动Self operator(){--_it;return *this;}Self operator(int){Self temp(*this);--_it;return temp;}Self operator--(){_it;return *this;}Self operator--(int){Self temp(*this);_it;return temp;}//// 迭代器支持比较bool operator!(const Self l)const{return _it ! l._it;}bool operator(const Self l)const{return _it ! l._it;}Iterator _it;};templateclass Tclass list{typedef ListNodeT Node;public:// 正向迭代器typedef ListIteratorT, T, T* iterator;typedef ListIteratorT, const T, const T const_iterator;// 反向迭代器typedef ReverseListIteratoriterator reverse_iterator;typedef ReverseListIteratorconst_iterator const_reverse_iterator;public:///// List的构造list(){CreateHead();}list(int n, const T value T()){CreateHead();for (int i 0; i n; i)push_back(value);}template class Iteratorlist(Iterator first, Iterator last){CreateHead();while (first ! last){push_back(*first);first;}}list(const listT l){CreateHead();// 用l中的元素构造临时的temp,然后与当前对象交换listT temp(l.begin(), l.end());this-swap(temp);}listT operator(listT l){this-swap(l);return *this;}~list(){clear();delete _head;_head nullptr;}///// List的迭代器iterator begin() { return iterator(_head-_next); }iterator end() { return iterator(_head); }const_iterator begin()const { return const_iterator(_head-_next); }const_iterator end()const{ return const_iterator(_head); }reverse_iterator rbegin(){return reverse_iterator(end());}reverse_iterator rend(){return reverse_iterator(begin());}const_reverse_iterator rbegin()const{return const_reverse_iterator(end());}const_reverse_iterator rend()const{return const_reverse_iterator(begin());}///// List的容量相关size_t size()const{Node* cur _head-_next;size_t count 0;while (cur ! _head){count;cur cur-_next;}return count;}bool empty()const{return _head-_next _head;}void resize(size_t newsize, const T data T()){size_t oldsize size();if (newsize oldsize){// 有效元素个数减少到newsizewhile (newsize oldsize){pop_back();oldsize--;}}else{while (oldsize newsize){push_back(data);oldsize;}}}// List的元素访问操作// 注意List不支持operator[]T front(){return _head-_next-_val;}const T front()const{return _head-_next-_val;}T back(){return _head-_prev-_val;}const T back()const{return _head-_prev-_val;}// List的插入和删除void push_back(const T val) { insert(end(), val); }void pop_back() { erase(--end()); }void push_front(const T val) { insert(begin(), val); }void pop_front() { erase(begin()); }// 在pos位置前插入值为val的节点iterator insert(iterator pos, const T val){Node* pNewNode new Node(val);Node* pCur pos._node;// 先将新节点插入pNewNode-_prev pCur-_prev;pNewNode-_next pCur;pNewNode-_prev-_next pNewNode;pCur-_prev pNewNode;return iterator(pNewNode);}// 删除pos位置的节点返回该节点的下一个位置iterator erase(iterator pos){// 找到待删除的节点Node* pDel pos._node;Node* pRet pDel-_next;// 将该节点从链表中拆下来并删除pDel-_prev-_next pDel-_next;pDel-_next-_prev pDel-_prev;delete pDel;return iterator(pRet);}void clear(){Node* cur _head-_next;// 采用头删除删除while (cur ! _head){_head-_next cur-_next;delete cur;cur _head-_next;}_head-_next _head-_prev _head;}void swap(bite::listT l){std::swap(_head, l._head);}private:void CreateHead(){_head new Node;_head-_prev _head;_head-_next _head;}private:Node* _head;};
}///
// 对模拟实现的list进行测试
// 正向打印链表
templateclass T
void PrintList(const bite::listT l)
{auto it l.begin();while (it ! l.end()){cout *it ;it;}cout endl;
}
4.list与vector的对比 vector与list都是STL中非常重要的序列式容器由于两个容器的底层结构不同导致其特性以及 应用场景不同其主要不同如下 vectorlist底层结构动态顺序表一段连续空间带头结点的双向循环链表随机访问支持随机访问访问某个元素效率O(1)不支持随机访问访问某个元素效率O(N)插入和删除任意位置插入和删除效率低需要搬移元素时间复杂度为O(N)插入是有可能需要增容增容开辟新空间拷贝元素释放旧空间导致效率更低任意位置插入和删除效率高不需要搬移元素时间复杂度O(1)空间利用率底层为连续空间不容易造成内存碎片空间利用率高缓存利用率高底层结点动态开辟小结点容易造成内存碎片缓存利用率低迭代器原生态指针对原生态指针结点指针进行封装迭代器失效在插入元素时要给所有的迭代器重新赋值因为插入元素有可能会导致重新扩容使原来迭代器失效删除时当前迭代器需要迭代器重新赋值否则会失效插入元素不好导致迭代器失效删除元素时只会导致当前迭代器失效其他迭代器不受影响使用场景需要高效存储支持随机访问不关心插入删除效率大量插入和删除操作不关心随机访问 感谢