Vector的基本功能实现与算法

功能描述:

  1. 向量构造和析构
  2. 向量扩容操作
  3. 无序向量查找
  4. 无序向量区间删除/单个删除
  5. 无序向量去重
  6. 无序向量插入
  7. 判断向量是否有序
  8. 有序向量查找
  9. 有序向量去重
  10. 二分查找
  11. 插值查找
  12. 起泡排序
  13. 归并排序

其中,二分查找做了三个版本,可以比较看各版本的差异;起泡排序做来两个版本,新版本比旧版本效率更高。

#ifndef VECTOR_H
#define VECTOR_H
typedef int rank;
#define DEFAULT_CAPACITY 3
#include <iostream>

template <typename T>
class Vector
{
   
  private:
    T *elem;
    rank size;
    int capacity;

  protected:
  public:
    Vector(int c = DEFAULT_CAPACITY) //默认构造函数
    {
   
        elem = new T(capacity = c);
        size = 0;
    }
    Vector(T const *A, rank lo, rank hi) {
    copyFrom(A, lo, hi);}  
    Vector(Vector<T> const V, rank lo, rank hi) {
    copyFrom(V.elem, lo, hi); }
    Vector(Vector<T> const &V) {
    copyFrom(V.elem, V.size); }//复制构造函数
    ~Vector() {
    delete[] elem; }
    Vector<T> &operator=(Vector<T> const &V);   //重载运算符
    T &operator[](rank r);
    friend std::ostream &operator<<(std::ostream &os, const Vector<T> V);
    void copyFrom(T const *A, rank lo, rank hi);
    void copyFrom(T const *A, rank s);
    void expand();  //扩容操作
    T max(T x, T y);
    rank insert(rank r, T const& e); //插入操作
    int remove(rank lo, rank hi); //区间删除
    T remove(rank r);
    rank find(T const &e, rank lo, rank hi)const; //无序vector中的查找
    int deduplicate(); //无序vector中的去重
    void traverse(void (*visit)(T &));
    struct increase
    {
   
        virtual void operator()(T &e) {
    e++; }
    };
    struct visit
    {
   
        virtual void operator()(T *arr){
   arr;}
    };
    int disordered()const;//判断是否有序
    int uniquity();//有序vector中的去重
    rank search(T const &e, rank lo, rank hi);//有序vector中的查找
    static rank A_Version_binSearchbinSearch(T * A, T const &e, rank lo, rank hi);//二分查找A版本
    static rank B_Version_binSearchbinSearch(T * A, T const &e, rank lo, rank hi);//二分查找B版本
    static rank C_Version_binSearchbinSearch(T *A, T const &e, rank lo, rank hi);//二分查找的C版本
    static rank interpolationSearch(T *A, const int &e, rank lo, rank hi);
    void bubblesort(rank lo, rank hi);//起泡排序
    void new_bubblesort(rank lo, rank hi);//起泡排序新版本
    bool 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值