同vector一样,list也是常用的一种STL容器。
list为双线列表,能够快读的插入和删除元素,在实际项目中也是应用广泛,但不支持随机访问,已有接口不够丰富,或是缺少常用的接口,于是本文意在原list基础上,改进或新增应用接口。
本文源码见【完整代码】章节,或GitHub:https://github.com/deargo/cpphelper。
list介绍
list 也是顺序容器的一种,它是一个双向链表(doubly-linked list)。
list将元素按顺序存储在链表中,表的每个元素中都有一个指针指向后一个元素,也有一个指针指向前一个元素,如下图所示。
当然,list的用法和vector很类似,也拥有顺序容器中的常用方法,但需要注意的是,list不支持使用下标随机存取元素。
在 list 容器中,在已经定位到要增删元素的位置的情况下,只需修改几个指针而已,增删元素能在常数时间内完成。如下图所示,在 ai 和 ai+1 之间插入一个元素,只需要修改 ai 和 ai+1 中的指针即可:
所以它和向量(vector)相比,能够快速的插入和删除,但是,随机访问却比较慢。
接口改进
list的许多函数,其用法都与 vector 类似,除了顺序容器都有的成员函数外,还独有push_front、pop_front、sort、remove、unique、merge、splice等接口,这给我们提供了遍历,但部分接口不够丰富,需要拓展。
由于自身结构的问题,list不支持随机访问,所以不支持随机访问迭代器,不支持下标操作符 [ ]、at等随机访问接口。虽然随机访问不够效率,代价大,但还是不免使用到这种场景,需要新增。
完整代码
本文源码见【完整代码】章节,或GitHub:https://github.com/deargo/cpphelper。
#pragma once
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
#include <list>
#include <set>
#include <vector>
#include <algorithm>
#include <functional>
#include <assert.h>
namespace CppHelper
{
template<class Type , class Alloc = std::allocator<Type> >
class CList : public std::list<Type,Alloc>{
using StdList = std::list<Type,Alloc>;
public:
explicit CList(const Alloc& alloc = Alloc()):StdList(alloc) {} //默认构造函数
explicit CList(typename CList::size_type n, const Type& val = Type(), const Alloc& alloc = Alloc()):StdList(n,val,alloc){} //容量构造函数
CList(CList&& other):StdList(std::forward<CList>(other)){ } //拷贝构造函数
CList(const std::list<Type,Alloc>& vec){ StdList::assign(vec.begin(), vec.end()); } //可用std::list初始化
template <class InitIterator> //可用迭代器初始化
CList (InitIterator first, InitIterator last, const Alloc& alloc = Alloc()):StdList(first,last,alloc){}
explicit CList(const std::set<Type>& set){ StdList::assign(set.begin(), set.end()); } //可用std::set初始化
explicit CList(const std::vector<Type>& vec){ StdList::assign(vec.begin(), vec.end()); } //可用std::vector初始化
public:
operator std::list<Type,Alloc>(){ return *this; } //类型转换函数,可直接转换为std::list
bool operator == (const CList& other) const{ return StdList::size()==other.size() && std::equal(StdList::begin(),StdList::end(),other.begin()); }
bool operator !=(const CList& other) const { return StdList::size()!=other.size() || false == std::equal(StdList::begin(),StdList::end(),other.begin()); }
Type& operator [](typename CList::size_type index){ assert(index<StdList::size()); typename CList::iterator it = CList::begin(); while(index--) ++it; return *it; }
const Type& operator [](typename CList::size_type index) const { assert(index<StdList::size());typename CList::const_iterator it = CList::begin(); while(index--) ++it; return *it; }
CList& operator =(const CList& other){StdList::assign(other.begin(), other.end()); return *this;} //赋值构造函数
CList& operator +(const CList& other){ push_back(other); return *this; }
CList& operator +=(Type&& value){ push_back(std::forward<Type>(value)); return *this; }
CList& operator +=(const CList& other){ push_back(other);