c++ 模仿Vectors类
#include <iostream>
using namespace std;
//自定义容器类
template<typename T>
class MyVectors{
public:
//无参构造
MyVectors():_start(0),_end(0),_size(0){
_data = new T[_capacity];
}
//析构函数
~MyVectors(){
delete [] _data;
}
//拷贝构造函数
MyVectors(const MyVectors &other):_start(other._start),_end(other._end),_size(other._size),_capacity(other._capacity){
_data = new T[_capacity];
for(size_t i =_start;i<_end;i++){
_data[i] = other._data[i];
}
}
//拷贝赋值函数
const MyVectors &operator=(const MyVectors &other){
if(this != &other){
_start = other._start;
_end = other._end;
_size = other._size;
_capacity = other._capacity;
delete [] _data;
_data = new T[_capacity];
for(size_t i =_start;i<_end;i++){
_data[i] = other._data[i];
}
}
return *this;
}
//将区间[start, end)的元素赋到当前vector
void assign(T *start,T *end){
size_t count = end-start;//计算元素个数
size_t newCapacity = _capacity;
while(newCapacity < count){
//扩容2倍
newCapacity = newCapacity * 2;
}
T *new_data = new T[newCapacity];
size_t i=0;
while(i < count){
new_data[i] = *(start+i);
i++;
}
//复制完毕后,更新类成员
delete [] _data;
_data = new_data;
_start = 0;
_end = count;
_size = count;
_capacity = newCapacity;
}
//赋num个值为val的元素到vector中
void assign(size_t num,const T &val){
size_t newCapacity = _capacity;
while(newCapacity < num){
//扩容2倍
newCapacity = newCapacity * 2;
}
T *new_data = new T[newCapacity];
size_t i=0;
while(i < num){
new_data[i] = val;
i++;
}
//复制完毕后,更新类成员
delete [] _data;
_data = new_data;
_start = 0;
_end = num;
_size = num;
_capacity = newCapacity;
}
//返回指定位置的元素
const T &at(size_t index) const{
if(index >= _size || index < 0){
throw out_of_range("An error occurred!");
}
return _data[_start+index];
}
//重载[]返回指定位置的元素
const T& operator[](size_t index) const {
if(index >= _size || index < 0){
throw out_of_range("An error occurred!");
}
return _data[_start+index];
}
//返回最末一个元素
const T &back() const{
if(empty()){
throw out_of_range("An error occurred!");
}
return _data[_end-1];
}
//返回vector所能容纳的元素数量
size_t capacity() const{
return _capacity;
}
//删除当前vector中的所有元素
void clear(){
_start = 0;
_end = 0;
_size = 0;
}
//判断Vector是否为空
bool empty() const{
return _size == 0;
}
//删除指定元素
void erase(size_t pos){
if(empty() || pos < 0 || pos>=_size){
throw out_of_range("An error occurred!");
}
for(size_t i =_start+pos;i<_end-1;i++){
_data[i] = _data[i+1];
}
_end--;
_size--;
}
//删除区间[start, end)的所有元素
void erase(size_t start,size_t end){
if(empty() || start < 0 || end <0 || start>=_size || start >= end){
throw out_of_range("An error occurred!");
}
//允许end>_size,取2者最小值
end = end>=_size?_size:end;
size_t i = _start + start;//开始位置
size_t j = _start + end;
while(j<_end){
_data[i] = _data[j];
i++;
j++;
}
_end = i;
_size -= end - start;
}
//返回第一个元素
const T &front() const{
if(empty()){
throw out_of_range("An error occurred!");
}
return _data[_start];
}
//在指定位置loc前插入区间[start, end)的所有元素
void insert(size_t loc,T *start,T *end){
//判断loc是否越界
if(loc<0 || loc>=_size){
throw out_of_range("An error occurred!");
}
size_t count = end-start;//插入个数
size_t newCapacity = _capacity;//新的容量
while(newCapacity < _end+count){
//扩容2倍
newCapacity = newCapacity * 2;
}
T *new_data = new T[newCapacity];
size_t i,index=0;
//先复制loc之前的元素
i=0;
while(i<loc){
new_data[index] = _data[_start+i];
index++;
i++;
}
//插入新的内容
i=0;
while(i < count){
new_data[index] = *(start+i);
index++;
i++;
}
i=loc;
while(i < _size){
new_data[index] = _data[_start+i];
index++;
i++;
}
//复制完毕后,更新类成员
delete [] _data;
_data = new_data;
_end = _end+count;
_size = _size+count;
_capacity = newCapacity;
}
//在指定位置loc前插入num个值为val的元素
void insert(size_t loc,size_t num,const T &val){
//判断loc是否越界
if(loc<0 || loc>=_size){
throw out_of_range("An error occurred!");
}
size_t count = num;//插入个数
size_t newCapacity = _capacity;//新的容量
while(newCapacity < _end+count){
//扩容2倍
newCapacity = newCapacity * 2;
}
T *new_data = new T[newCapacity];
size_t i,index=0;
//先复制loc之前的元素
i=0;
while(i<loc){
new_data[index] = _data[_start+i];
index++;
i++;
}
//插入新的内容
i=0;
while(i < count){
new_data[index] = val;
index++;
i++;
}
i=loc;
while(i < _size){
new_data[index] = _data[_start+i];
index++;
i++;
}
//复制完毕后,更新类成员
delete [] _data;
_data = new_data;
_end = _end+count;
_size = _size+count;
_capacity = newCapacity;
}
//移除最后一个元素
void pop_back(){
cout<<"pop_back ";
if(empty()){
throw out_of_range("An error occurred!");
}
cout<<_data[_end-1]<<endl;
_end--;
_size--;
}
//在Vector最后添加一个元素
void push_back(const T &val){
cout<<"push_back "<<val<<endl;
if(_size == _capacity){
expand_capacity();
}
_data[_end] = val;
_end++;
_size++;
}
//数据遍历
void show() const{
cout<<"*****1*****"<<endl;
cout<<"MyVectors.size="<<_size<<endl;
cout<<"MyVectors.capacity="<<_capacity<<endl;
cout<<"MyVectors:";
for(size_t i=_start;i<_end;i++){
cout<<_data[i]<<" ";
}
cout<<endl;
cout<<"*****2*****"<<endl;
}
const T* begin() const{
return &_data[_start];
}
const T* end() const{
return &_data[_start];
}
//返回Vector元素数量的大小
size_t size() const{
return _size;
}
//扩容
void expand_capacity(){
cout<<"expand_capacity()"<<endl;
size_t newCapacity = _capacity*2;//扩容
T * new_data = new T[newCapacity];//申请新的空间
//复制数据
size_t i = _start;
while(i < _end){
new_data[i] = _data[i];
i++;
}
delete [] _data;//删除原数据
_data = new_data;//替换成新数据
_capacity = newCapacity;//替换成新的容量
}
//交换两个MyVectors内容
void swap(MyVectors &from ){
MyVectors newOne(from);
from = *this;
*this = newOne;
}
private:
//数据范围[_start,_end)
T *_data;
size_t _start;//开始位置下标
size_t _end;//结束位置下标
size_t _size;//元素个数
size_t _capacity=5;//默认容量为5
};
int main()
{
MyVectors<int> vector_1;
MyVectors<int> vector_2;
int a[6] = {1,2,3,4,5,6};
cout<<"vector_1.assign(a,a+6):"<<endl;
vector_1.assign(a,a+6);
vector_1.show();
cout<<"vector_1.erase(1,2):"<<endl;
vector_1.erase(1,2);
vector_1.show();
cout<<"vector_1.insert(1,4,9):"<<endl;
vector_1.insert(1,4,9);
vector_1.show();
cout<<"vector_1.assign(5,2):"<<endl;
vector_2.assign(5,2);
vector_2.show();
vector_2.push_back(3);
vector_2.show();
cout<<"vector_2.capacity()="<<vector_2.capacity()<<endl;
cout<<"vector_2.at(5)="<<vector_2.at(5)<<endl;
cout<<"vector_2[5]="<<vector_2[5]<<endl;
cout<<"vector_2.erase(5):"<<endl;
vector_2.erase(5);
vector_2.show();
cout<<"vector_2.insert(0,a,a+6):"<<endl;
vector_2.insert(0,a,a+6);
vector_2.show();
vector_2.pop_back();
vector_2.show();
cout<<"vector_2.swap(vector_1)"<<endl;
vector_2.swap(vector_1);
vector_1.show();
vector_2.show();
return 0;
}
vector_1.assign(a,a+6):
*****1*****
MyVectors.size=6
MyVectors.capacity=10
MyVectors:1 2 3 4 5 6
*****2*****
vector_1.erase(1,2):
*****1*****
MyVectors.size=5
MyVectors.capacity=10
MyVectors:1 3 4 5 6
*****2*****
vector_1.insert(1,4,9):
*****1*****
MyVectors.size=9
MyVectors.capacity=10
MyVectors:1 9 9 9 9 3 4 5 6
*****2*****
vector_1.assign(5,2):
*****1*****
MyVectors.size=5
MyVectors.capacity=5
MyVectors:2 2 2 2 2
*****2*****
push_back 3
expand_capacity()
*****1*****
MyVectors.size=6
MyVectors.capacity=10
MyVectors:2 2 2 2 2 3
*****2*****
vector_2.capacity()=10
vector_2.at(5)=3
vector_2[5]=3
vector_2.erase(5):
*****1*****
MyVectors.size=5
MyVectors.capacity=10
MyVectors:2 2 2 2 2
*****2*****
vector_2.insert(0,a,a+6):
*****1*****
MyVectors.size=11
MyVectors.capacity=20
MyVectors:1 2 3 4 5 6 2 2 2 2 2
*****2*****
pop_back 2
*****1*****
MyVectors.size=10
MyVectors.capacity=20
MyVectors:1 2 3 4 5 6 2 2 2 2
*****2*****
vector_2.swap(vector_1)
*****1*****
MyVectors.size=10
MyVectors.capacity=20
MyVectors:1 2 3 4 5 6 2 2 2 2
*****2*****
*****1*****
MyVectors.size=9
MyVectors.capacity=10
MyVectors:1 9 9 9 9 3 4 5 6
*****2*****