list介绍
list模板类也是一个容器。list是由双向链表实现,每个节点存储一个元素。list支持前后两种移动方向。
list优势在于可以在任何位置上进行插入、删除,同时也可以双向迭代。

list与vector的区别

  • list不支持随机访问
  • list没有下标访问符[]和at()函数
  • list没有提供容量,空间操作的函数,每个元素都有自己的内存。list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
  • list的插入、删除不会影响迭代器的失效

list构造函数

list() 构造空的 list
list(size) 创建大小为 size 的对象
list(size , value) 创建大小为 size 的对象,每个元素的初始值都为 value
list(const list& l) 拷贝构造
list(first , end) 用[first , end)区间的元素创建 list

元素的赋值

push_front() 在容器的头部插入新的元素
push_back() 在容器的尾部插入新的元素
pop_front() 删除头元素
pop_back() 删除尾元素
//函数原型
void push_front(const T& x);
void push_back(const T& x);
void pop_front();
void pop_back();

容器的容量
resize()、size()、max_size()作用于vector容器中的作用类似。
max_size()为容器最大

特殊函数
merge(),将2个list对象合并成文一个list对象,合并后的容器中的元素是按照升序排列。
sort(),用于list类型容器中的元素进行排序,默认的排序方式为从小到大。

void sort();
void sort(greater<T> x);//从大到小排序

remove(),不需要指定位置,只需要告诉删除元素的值,就可以直接删除相应的元素。
unique(),移除相邻重复的元素

l1.unique();  //删除相邻重复的元素
l2.unique(Pred);//仅保留与第一个元素相等的元素

如果用unique()函数,删除重复的元素时,应该先给对象进行排序。

reverse(),翻转字符串

#include<iostream>using namespace std;namespace Bai
{template<class T>class ListNode{public:ListNode(const T& value = T()):val (value),_pnext(nullptr),_prev(nullptr){}T val;ListNode<T>* _pnext;ListNode<T>* _prev;};template<class T,class Ref,class Ptr>class ListIterator{public:typedef ListNode<T>* PNode;typedef ListIterator<T, Ref, Ptr>Self;PNode _node;ListIterator(PNode node):_node(node){}Ref operator*(){return _node->val;}Ptr operator->(){return &(_node->val);}Self& operator++(){_node = _node->_pnext;return *this;}Self operator++(int){Self temp(*this);_node = _node->_pnext;return temp;}Self operator--(){_node = _node->_prev;return *this;}Self operator--(int){Self temp(*this);_node = _node->_prev;return temp;}bool operator!=(const Self& x){return _node != x._node;}bool operator==(const Self& x){return _node == x._node;}};template<class T>class List{public:typedef ListNode<T> Node;typedef Node* PNode;typedef ListIterator<T, T&, T*> iterator;List(){_head = new Node;_head->_pnext = _head;_head->_prev = _head;}List(int n, const T& val = T()){_head = new Node;_head->_pnext = _head;_head->_prev = _head;for (int i = 0; i < n; ++i){PushBack(val);}}template<class Iterator>List(Iterator first, Iterator last){_head = new Node;_head->_pnext = _head;_head->_prev = _head;while (first != last){PushBack(*first);first++;}}List(const List<T>& x){_head = new Node;_head->_pnext = _head;_head->_prev = _head;List<T> temp(x.begin(), x.end());swap(_head, temp);}List<T>& operator=(List<T> x){swap(_head, x._head);return *this;}~List(){if (_head){PNode pcur = _head->_pnext;while (pcur != _head){PNode next = pcur->_pnext;delete pcur;pcur = next;}delete _head;_head = nullptr;}}iterator begin(){return iterator(_head->_pnext);}iterator end(){return iterator(_head);}T& Front(){return _head->_pnext->val;}const T& Front()const{return _head->_pnext->val;}T& Back(){return _head->_prev->val;}void PushBack(const T& val){PNode NewNode = new Node(val);PNode prev = _head->_prev;prev->_pnext = NewNode;NewNode->_prev = prev;NewNode->_pnext = _head;_head->_prev = NewNode;}void PopBack(){PNode del = _head->_prev;if (del != _head){_head->_prev = del->_prev;del->_prev->_pnext = _head;delete del;}}void PushFront(const T& val){PNode NewNode = new Node(val);NewNode->_prev = _head;NewNode->_pnext = _head->_pnext;_head->_pnext->_prev = NewNode;_head->_pnext = NewNode;}void PopFront(){PNode cur = _head->_pnext;if (cur != _head){_head->_pnext = cur->_pnext;cur->_pnext->_prev = _head;delete cur;} }//在pos位置前插入valiterator Insert(iterator pos, const T& val){PNode NewNode = new Node(val);PNode pCur = pos._node;NewNode->_pnext = pCur;NewNode->_prev = pCur->_prev;NewNode->_prev->_pnext = NewNode;pCur->_prev = NewNode;}iterator Erase(iterator pos){PNode pCur = pos._node;PNode prev = pCur->_prev;PNode next = pCur->_pnext;if (pCur != _head){prev->_pnext = next;next->_prev = pre;delete pCur;pCur = nullptr;pos = iterator(next);}return next;}void Clear(){PNode cur = _head->_pnext;while (cur != _head){PNode temp = cur->_pnext;delete cur;cur = temp;}_head->_pnext = _head;_head->_prev = _head;}size_t Size(){size_t size;PNode cur = _head->_pnext;while (cur != _head){size++;cur = cur->_pnext;}return size;}bool Empty()const{return _head->_pnext == _head;}void Resize(size_t size, const T& val = T()){size_t Oldsize = Size();if (Oldsize <= size){for (size_t i = Oldsize; i < size; ++i)PushBack(val);}else{for (size_t i = size; i > Oldsize; --i)PopBack();}}private:PNode _head;};
}template<class T>
void PrintList(Bai::List<T>& x)
{auto it = x.begin();while (it != x.end()){cout << *it << " ";it++;}
}
int main()
{int array[] = { 1,2,3,4,5,6,7,8,9 };Bai::List<int> L(array, array + sizeof(array) / sizeof(array[0]));PrintList(L);system("pause");return 0;
}

C++---list(列表)模板相关推荐

  1. SharePonit 2010 更改另存为列表模板的语言类型

    从朋友处得来一个列表模板:AccessApplicationSharePoint.stp 将其通过:网站操作----网站设置----列表模板,上传进去.然后去创建列表,发现找不到此模板. 根据多年老司 ...

  2. stm32f10x_it.c 定义的程序列表模板(stm32f103x_it.c中放的是中断的空函数)

    1.stm32f103x_it.c中放的是中断的空函数 2.STM32是如何进入中断程序的:     首先,程序是放在0x80000000-0x8001ffff FLASH所在的代码段内:SRAM从地 ...

  3. html文字列表,文字列表模板

    文字列表模板 1.如何编辑列表模板 文字列表模板存放在模板包archive文件夹中,命名以list_text为前缀 在模板包中找到list_text.html 模板,复制另存为一个新模板,命名为lis ...

  4. 图片列表模板 html代码,图片列表模板

    图片列表模板 1.如何编辑图片列表模板 图片列表模板存放在模板包archive文件夹中,命名以list_pic为前缀 在模板包中找到list_pic.html 模板,复制另存为一个新模板,命名为lis ...

  5. 帝国cms:评论列表模板中调用热门文章或是推荐文章的js方法

    后台管理评论模板里有评论的列表模板,想在评论列表模板中调用热门文章或是推荐文章,现在考虑到标签和js. 长知识:动态模板无法使用标签,所以标签在这里就pass了,专心搞js----> 1.后台= ...

  6. 商品列表页面 html,商品列表模板

    商品列表模板 1.如何编辑商品列表模板 商品列表模板存放在模板包archive文件夹中,命名以list_product为前缀 在模板包中找到list_product.html 模板,复制另存为一个新模 ...

  7. 列表找房(十)02-渲染列表模板

    渲染列表模板 列表样式调整 // find/index.scss .header 去掉绝对定位 /* position: absolute; */// Filter/index.module.css ...

  8. 小程序 小程序列表模板

    小程序列表模板 今天做小程序列表页面 百度搜了一圈也没有一个模板 今天花了两个小时自己做了一个 放图 ! 中间的隔离线在手机显示的时候颜色比较浅 截图上面显示的比较深 数据是死的 弄成活的写for就可 ...

  9. html模板 图片文列表,图片列表模板

    图片列表模板 1.如何编辑图片列表模板 图片列表模板存放在模板包archive文件夹中,命名以list_pic为前缀 在模板包中找到list_pic.html 模板,复制另存为一个新模板,命名为lis ...

  10. dedecms 封面模板 列表模板 文章模板

    dedecms 封面模板 列表模板 文章模板 封面模板可以理解为点击一级栏目后出现的模板页面:封面是单页,就是你这个栏目不需要列表,只有一些文字内容. 调用出封面模板index_article1.ht ...

最新文章

  1. linux ubuntu桌面进程,如何加快你的Ubuntu桌面性能
  2. oracle 日结 数据量大,如何对一个oracle11gsql语
  3. 初学计算几何(四)——初识凸包
  4. C#总结项目《汽车租聘系统》项目代码实例【全注释版】
  5. mysql 导致iis 假死_解决IIS无响应假死状态
  6. TCP传输过程中丢包问题
  7. Linux运维学习大纲
  8. 理解搜索引擎并且善用google
  9. 蓝桥杯 穿越雷区(bfs)
  10. Mybatis原理分析之二:框架整体设计
  11. 禁止前端页面用户打开HTML页面调试功能的初级方法
  12. 计算机制造商logo,如何更改系统oem制造商logo等信息
  13. 数据分析必看的oCPM/oCPC核心知识点
  14. UML工具Visual Paradigm最新版本16.2发布
  15. signature=b4b1c7e18770785c0aa672d85aa24d2b,Surveying Extended GMSB Models with mh=125 GeV
  16. 小目标检测的一些理解
  17. 友华改设备标识命令_电信路由器密码怎么修改,路由器牌子是友华通信
  18. 网络大厂与加州大学分校开发出Actor-Critic算法
  19. /usr/bin/ld: cannot find -lxxx的错误
  20. 个人商业模式,如何让自己变得值钱

热门文章

  1. 电脑出现 flash update failed 解决方法
  2. 使用即时通讯(IM)SDK心得体验
  3. 【JAVASCRIPT】javascript获取屏幕,浏览器,网页高度宽度
  4. 做你最后的Administrator
  5. A2W和W2A :很好的多字节和宽字节字符串的转换宏
  6. 【工程项目经验】之Xcode的Instruments检测解决iOS内存泄露
  7. Linux比windows生态好,试装国产统一系统UOS:操作比Windows更简便,但生态依然是缺点,...
  8. Redis-数据结构04-整数集合(intset)
  9. mysql重装第四步报错_Mysql 8.x 安装
  10. mt4交易系统源码_mt4周边:一款免费的数据下载工具