zhx6044的个人主页

http://www.qtcn.org/bbs/u/118368  [收藏] [复制]

zhx6044

  • 12

    关注

  • 4

    粉丝

  • 28

    访客

  • 等级:新手上路
  • 总积分:28
  • 保密,2011-06-21

最后登录:2015-05-27

更多资料

日志

myVector

2012-06-04 22:33
    这几天把数据结构重新拿出了看,有个想法,给自己写个小的STL,这是第一个容器,向量
   #ifndef MYVECTOR_H
#define MYVECTOR_H
#include <iostream>
using namespace std;
template<typename T>
class myVector
{
public:
    myVector(int capacity = DEFINE_SIZE);
    ~myVector();
    myVector(const myVector<T>& co);
    myVector<T>& operator=(const myVector<T>&co);
    int size() const {return _size;}
    int capacity() const {return _capacity;}
    T* elems() const{return p_elems;}
    bool isEmpty() const {return _size <= 0;}
    void insert(const T& elem);
    //r,f的取值在0~_size-1之间
    T& operator [](int r ) const;
    int remove(int r);
    int remove(int f,int l);
    void sort() {sort(0,_size);}
    void sort(int l, int r);
protected:
    void expandCapacity();
    void copyOfRange(T* arr,int f,int l);//f是起始位,l是长度(包括f)
    //   void insertSort(int l,int r);
    //   void swap(int& a,int& b);
private:
    static const int DEFINE_SIZE = 3;
    int _size;
    int _capacity;
    T *p_elems;
};
#endif //MYVECTOR_H
template<typename T>
myVector<T>::myVector(int capacity):_size(0), _capacity(capacity),p_elems(new T[_capacity])
{
}
template<typename T>
myVector<T>::~myVector()
{
    delete []p_elems;
}

template<typename T>
myVector<T>::myVector(const myVector<T>& co)
{
    copyOfRange(co.p_elems,0,co._size);
}

template<typename T>
myVector<T>& myVector<T>::operator=(const myVector<T>& co)
{
    int n = co.size();
    if(_capacity <= n)
    {
        delete []p_elems;
        copyOfRange(co.elems(),0,co.size());
    }
    else
    {
        _size = n;
        while(n-- >= 0)
        {
            p_elems[n] = co.elems()[n];
        }
    }
    return *this;
}

template<typename T>
void myVector<T>::insert(const T& elem)
{
    int local = _size;
    if(_size == _capacity)
    {
        expandCapacity();
        p_elems[local] = elem;
    }
    else
    {
        p_elems[_size] = elem;
    }
    ++_size;
}

template<typename T>
T& myVector<T>::operator[](int r) const
{
    if(r > _size-1 || r < 0)
    {
        throw "error";
    }
    else
    {
        return p_elems[r];
    }
}
template<typename T>
int myVector<T>::remove(int r)
{
    remove(r,1);
    return r;
}

template<typename T>
int myVector<T>::remove(int f, int l)
{
    if(f < 0||f > _size-1)
    {
        return -1;
    }
    if(f-1+l >= _size-1)
    {
        _size = f;
    }
    else{
        int n = f+l;
        int m = f;
        while(n < _size && f<l+m)
        {
            p_elems[f++] = p_elems[n++];
        }
        _size -= l;
    }
    return 1;

}

//template<typename T>
//void myVector<T>::insertSort(int l, int r)
//{



//}
template<typename T>
void myVector<T>::expandCapacity()
{
    _capacity *= 2;
    T *temp = p_elems;
    p_elems = new T[_capacity];
    int n = _size;
    while(n-- >= 0)
    {
        p_elems[n] = temp[n];
    }
    delete temp;
}

template<typename T>
void myVector<T>::copyOfRange(T* arr,int f,int l)
{
    _capacity = l*2;
    _size = 0;
    p_elems = new T[_capacity];
    while(_size < l)
    {
        p_elems[_size++] = arr[f++];
    }
}
分类:C/C++|回复:0|浏览:1196|全站可见|转载
 

下一篇: 单模

上一篇: Qt中使用多线程

Powered by phpwind v8.7 Certificate Copyright Time now is:05-07 03:56
©2005-2016 QTCN开发网 版权所有 Gzip disabled