C++Helper--在原std::list基础上改进接口:新增、删除、查询、遍历、数据转换、集成算法等

  同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 中的指针即可:

C++Helper--在原std::list基础上改进接口:新增、删除、查询、遍历、数据转换、集成算法等_第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 
#include 
#include 
#include 
#include 
#include 

namespace CppHelper
{
template >
class CList : public std::list{
    using StdList = std::list;

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(other)){ }                                             //拷贝构造函数
    CList(const std::list& vec){ StdList::assign(vec.begin(), vec.end()); }                     //可用std::list初始化
    template                                                                            //可用迭代器初始化
    CList (InitIterator first, InitIterator last, const Alloc& alloc = Alloc()):StdList(first,last,alloc){}
    explicit CList(const std::set& set){ StdList::assign(set.begin(), set.end()); }                   //可用std::set初始化
    explicit CList(const std::vector& vec){ StdList::assign(vec.begin(), vec.end()); }                //可用std::vector初始化

public:
    operator std::list(){ 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(value)); return *this; }
    CList& operator +=(const CList& other){ push_back(other);

你可能感兴趣的:(C++Helper系列,C/C++,c++,stl,list)