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

  所以它和向量(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);
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
build-helper-maven-plugin:jar:1.8是一个用于构建Java项目的Maven插件。该插件提供了一些辅助功能,以帮助简化和优化项目构建过程。 首先,build-helper-maven-plugin:jar:1.8可以帮助我们在构建过程中处理一些非常规的任务。例如,它可以通过添加额外的源代码目录来扩展项目的结构,以便在构建过程中包含其他源代码。这对于多模块项目或需要集成外部代码库的项目非常有用。 此外,该插件还提供了一些用于处理资源文件的功能。我们可以使用该插件来复制或移动资源文件到特定的目录,以便在构建过程中正确地包含这些文件。这对于需要在构建期间处理和转换资源文件的项目非常有用,例如压缩JavaScript或CSS文件。 另一个重要的功能是build-helper-maven-plugin:jar:1.8可以帮助我们在构建过程中处理依赖关系。它可以自动将特定的依赖项添加到项目配置中,以便在编译和运行时正确地解决这些依赖项。这对于需要从其他模块或项目引入代码或库的项目非常有用。 除了以上提到的功能,build-helper-maven-plugin:jar:1.8还提供了其他一些辅助功能,如将构建生成的文件添加到构建输出中,生成版本号等。 综上所述,build-helper-maven-plugin:jar:1.8是一个功能强大的Maven插件,可以帮助我们更轻松地构建和管理Java项目。通过提供一些辅助功能,它提高了项目的灵活性和可维护性,使我们能够更加高效地进行构建过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值