类模板Vector

#ifndef Vector_hpp

#define Vector_hpp


#include <iostream>

using namespace std;


template<class T>

class Vector

{

private:

    T *_rep;

    int _capacity;

    int _size;

    Vector<T>& shrink();

public:

    Vector();

    Vector(T *p,int s);

    Vector(const Vector<T>& v);

    ~Vector();

    const Vector<T>& operator=(const Vector<T>& v);

    const T& operator [](int index)const;

    T& operator[](int index);

    int get_capacity()const;

    int get_size()const;

    Vector<T>& add(const T& t);

    T* search(const T& t)const;

    Vector<T>& remove(const T& t);

    Vector<T>& remove();

};

template<class T>

ostream& operator<<(ostream &out,const Vector<T>& v)

{

    for (int i=0; i<v.get_size(); ++i) {

        out<<v[i]<<endl;

    }

    return out;

}

template<class T>

Vector<T>& Vector<T>::shrink()

{

    _capacity=_capacity*3/4;

    T*temp=new T[_capacity];

    for (int i=0; i<_size; i++) {

        temp[i]=_rep[i];

    }

    delete []_rep;

    _rep=temp;

    return *this;

}

template<class T>

inline Vector<T>::Vector():_capacity(0),_size(0),_rep(NULL){}

template<class T>

Vector<T>::Vector(T *p,int s):_capacity(s),_size(s),_rep(new T[s])

{

    for (int i=0; i<s; i++) {

        _rep[i]=p[i];

    }

}

template<class T>

Vector<T>::Vector(const Vector<T>& v):_capacity(v._capacity),_size(v._size),_rep(new T[v._capacity])

{

    for (int i=0; i<_size; i++) {

        _rep[i]=v._rep[i];

    }

}

template<class T>

inline Vector<T>::~Vector()

{

    delete []_rep;

    _rep=NULL;

}

template<class T>

const Vector<T>& Vector<T>::operator=(const Vector<T>& v)

{

    if (_rep!=v._rep) {

        delete []_rep;

        _capacity=v._capacity;

        _size=v._size;

        _rep=new T[_capacity];

        for (int i=0; i<_size; i++) {

            _rep[i]=v._rep[i];

        }

    }

    return *this;

}

template<class T>

inline const T& Vector<T>::operator [](int index)const

{

    return _rep[index];

}

template<class T>

inline T& Vector<T>::operator[](int index)

{

    return _rep[index];

}

template<class T>

inline int Vector<T>::get_capacity()const

{

    return _capacity;

}

template<class T>

inline int Vector<T>::get_size()const

{

    return _size;

}

template<class T>

Vector<T>& Vector<T>::add(const T& t)

{

    if (_size==_capacity) {

        _capacity=2*_capacity+1;

        T*temp=new T[_capacity];

        for (int i=0; i<_size; i++) {

            temp[i]=_rep[i];

        }

        delete []_rep;

        _rep=temp;

    }

    _rep[_size++]=t;

    return *this;

}

template<class T>

T* Vector<T>::search(const T& t)const

{

    T*p=_rep;

    int i=0;

    while (i<_size&&*p!=t) {

        i++;

        p++;

    }

    if (i==_size) {

        p=NULL;

    }

    return p;

}

template<class T>

Vector<T>& Vector<T>::remove(const T& t)

{

    T*p=search(t);

    if (p) {

        for (int i=int(p-_rep); i<_size-1; i++) {

            _rep[i]=_rep[i+1];

        }

        --_size;

        if (_size<_capacity/2) {

            shrink();

        }

    }

    return *this;

}

template<class T>

Vector<T>& Vector<T>::remove()

{

    --_size;

    if (_size<_capacity/2) {

        shrink();

    }

    return *this;

}

template<class T>

bool operator==(const Vector<T>& v1,const Vector<T>& v2)

{

    bool yes=true;

    if (v1.get_capacity()!=v2.get_capacity()||v1.get_size()!=v2.get_size()) {

        yes=false;

    }

    else

    {

        int i=0;

        int s=v1.get_size();

        while (i<s&&v1[i]==v2[i++]) {};

        if (i<s) {

            yes=false;

        }

    }

    return yes;

}


#include <iostream>

#include "Vector.hpp"


int main(int argc, const char * argv[]) {

    

    Vector<int> Int_vec;

    Int_vec.add(1);

    Int_vec.add(2);

    Int_vec.add(3);

//    cout<<Int_vec<<endl;

    Vector<int> Int_Vec;

    Int_Vec = Int_vec;

//    cout<<"Int_Vec:\r"<<Int_Vec<<"Int_vec:\r"<<Int_vec<<endl;

//    if (Int_Vec == Int_vec) {

//        cout<<"相等"<<endl;

//    }

//    if (Int_vec.search(3)) {

//        cout<<"容器中存在该元素"<<endl;

//    }

//    else {

//        cout<<"容器中不存在该元素"<<endl;

//    }

//    Int_vec.remove(2);

//    cout<<Int_vec<<endl;

//    Int_vec.remove();

//    cout<<Int_vec<<endl;

//    cout<<Int_vec[1]<<endl;

    Int_vec.add(4);

    cout<<Int_vec.get_capacity()<<endl;

    cout<<Int_vec.get_size()<<endl;

    return 0;



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值