vector 排序_C++实现简单的vector数据结构

代码有点长,为了便于浏览,为大家奉上缩略图一张:

22e11cbf4df17303df775b31da288c27.png
vector.h

#ifndef VECTOR_H_

#define VECTOR_H_

#include"out_of_range.h" //这个是本人自定义的头文件,也可以选择用标准库

template<typename T>

class vector{

private:

using pointer=vector<T>*;

const static int DEFAULT_CAPACITY=8;

int _capacity=DEFAULT_CAPACITY;

int _size=0;

T* _elem=nullptr;

public:

vector():_elem(new T[_capacity]){}

vector(int count,const T& elem){

while(_capacity<count)_capacity<<=1;

_elem=new T[_capacity];

for(_size=0;_size<count;++_size)_elem[_size]=elem;

}

vector(const vector<T>& other){

while(_capacity<other.size())_capacity<<=1;

_elem=new T[_capacity];

for(_size=0;_size<other.size();++_size)_elem[_size]=other[_size];

}

vector(const std::initializer_list<T>& other){

while(_capacity<other.size())_capacity<<=1;

_elem=new T[_capacity];

for(const T& elem:other)_elem[_size++]=elem;

}

~vector(){delete[] _elem;}

private:

void expand(){

if(_size<_capacity)return;

_capacity<<=1;

T* oldElem=_elem;

_elem=new T[_capacity];

for(int i=0;i<_size;++i)_elem[i]=oldElem[i];

delete[] oldElem;

}

void quickSort(int begin,int end){//快速排序

if(begin==end)return;

int first=begin;

int last=end;

T target=_elem[begin];

while(first<last){

while(first<last&&_elem[last]>target)--last;

while(first<last&&_elem[first]<=target)++first;

if(first<last){

T temp=_elem[first];

_elem[first++]=_elem[last];

_elem[last--]=temp;

}

else if(_elem[first]>target){

_elem[begin]=_elem[first-1];

_elem[first-1]=target;

quickSort(begin,first-2);

quickSort(first,end);

return;

}

else{

_elem[begin]=_elem[first];

_elem[first]=target;

quickSort(begin,first-1);

quickSort(first+1,end);

return;

}

}

}

public:

int size()const{return _size;}

int capacity()const{return _capacity;}

bool empty()const{return _size<=0;}

void shrink_to_fit(){while((_capacity>>1>_size)&&(_capacity>>1>DEFAULT_CAPACITY))_capacity>>=1;}

public:

T& at(int index)const{

if(index<0||index>=_size)throw out_of_range("vector:out_of_range:at()");

return _elem[index];

}

T& front()const{return at(0);}

T& back()const{return at(_size-1);}

void insert(int index,const T& elem){

if(index<0||index>_size)throw out_of_range("out_of_range:vector:insert(int,const T&)");

if(_size==_capacity){

_capacity<<=1;

T* oldElem=_elem;

_elem=new T[_capacity];

for(int i=0;i<index;++i)_elem[i]=oldElem[i];

_elem[index]=elem;

for(int i=index+1;i<=_size;++i)_elem[i]=oldElem[i-1];

delete[] oldElem;

}

else{

for(int i=_size;i>index;--i)_elem[i]=_elem[i-1];

_elem[index]=elem;

}

++_size;

}

void insert(int index,const std::initializer_list<T>& other){

if(index<0||index>_size)throw out_of_range("vector:out_of_range:insert(int,std::initializer_list<T>&)");

if(_capacity<_size+other.size()){

while(_capacity<_size+other.size())_capacity<<=1;

T* oldElem=_elem;

_elem=new T[_capacity];

int i=0;

for(;i<index;++i)_elem[i]=oldElem[i];

for(const T& elem:other)_elem[index++]=elem;

while(i<_size)_elem[index++]=oldElem[i++];

delete[] oldElem;

}

else{

for(int i=_size-1+other.size();i>=index;--i)_elem[i]=_elem[i-other.size()];

for(const T& elem:other)_elem[index++]=elem;

}

_size+=other.size();

}

void push_back(const T& elem){insert(_size,elem);}

void push_front(const T& elem){insert(0,elem);}

void remove(int begin,int end){

int min=begin;

int max=end;

if(begin>end){min=end+1;max=begin+1;}

int count=max-min;

if(min<0||max>_size)throw out_of_range("out_of_range:vector:remove(int,int)");

for(int i=min;i<_size-count;++i)_elem[i]=_elem[i+count];

_size-=count;

shrink_to_fit();

}

T remove(int index){

if(index<0||index>=_size)throw out_of_range("vector:out_of_range:remove(int)");

T elem=_elem[index];

if(index!=_size-1)for(int i=index;i<_size-1;++i)_elem[i]=_elem[i+1];

--_size;

shrink_to_fit();

return elem;

}

T pop_back(){return remove(_size-1);}

T pop_front(){return remove(0);}

void clear(){

T* oldELem=_elem;

_capacity=DEFAULT_CAPACITY;

_size=0;

_elem=new T[_capacity];

delete[] oldELem;

}

void swap(vector<T>& other){

int i=0;

for(;i<_size&&i<other.size();++i){

T elem=_elem[i];

_elem[i]=other[i];

other[i]=elem;

}

if(i<_size){

int temp=other.size();

while(i<_size)other.push_back(_elem[i++]);

_size=temp;

shrink_to_fit();

}

else if(i<other.size()){

int temp=_size;

while(i<other.size())push_back(other.at(i++));

other.remove(temp, other.size());

}

}

int find(const T& elem)const{

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

if(_elem[i]==elem)return i;

return -1;

}

void unique(){

for(int m=0;m<_size-1;++m){

for(int n=m+1;n<_size;++n){

if(_elem[m]==_elem[n])remove(n--);

}

}

}

T max()const{

if(_size<=0)throw out_of_range("out_of_range:vector:max()");

T max=_elem[0];

for(int i=1;i<_size;++i)if(max<_elem[i])max=_elem[i];

return max;

}

T min()const{

if(_size<=0)throw out_of_range("vector:out_of_range:min()");

T min=_elem[0];

for(int i=1;i<_size;++i)if(min>_elem[i])min=_elem[i];

return min;

}

void sort(){return quickSort(0,_size-1);}

void reverse(){

for(int i=0,j=_size-1;i<j;++i,--j){

T temp=_elem[i];

_elem[i]=_elem[j];

_elem[j]=temp;

}

}

public:

T& operator[](int index)const {return at(index);}

vector<T>& operator=(const vector<T>& other){

for(_capacity=DEFAULT_CAPACITY;_capacity<other.size();_capacity<<=1);

T* oldElem=_elem;

_elem=new T[_capacity];

for(_size=0;_size<other.size();++_size)_elem[_size]=other[_size];

delete[] oldElem;

return *this;

}

bool operator==(const vector<T>& other){

if(_size!=other.size())return false;

for(int i=0;i<_size;++i)if(_elem[i]!=other[i])return false;

return true;

}

bool operator!=(const vector<T>& other){return !(*this==other);}

bool operator<(const vector<T>& other){

bool flag=false;

for(int i=0;i<_size&&i<other.size();++i){

if(_elem[i]>other.at(i))return false;

else if(_elem[i]!=other.at(i))flag=true;

}

return _size<other.size()||(_size==other.size()&&flag);

}

bool operator>(const vector<T>& other){return other<*this;}

bool operator<=(const vector<T>& other){return !(other<*this);}

bool operator>=(const vector<T>& other){return !(*this<other);}

vector<T>& operator+=(const vector<T>& other){

int newSize=_size+other.size();

if(_capacity<newSize){

do{_capacity<<=1;}while(_capacity<newSize);

T* oldElem=_elem;

_elem=new T[_capacity];

for(int i=0;i<_size;++i)_elem[i]=oldElem[i];

}

for(int i=0;i<other.size();++i)_elem[_size++]=other[i];

_size=newSize;

return *this;

}

vector<T> operator+(const vector<T>& other){

vector<T>result=*this;

result+=other;

return result;

}

};

#endif /* VECTOR_H_ */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值