这几天把数据结构重新拿出了看,有个想法,给自己写个小的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++];
}
}
#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++];
}
}