template <typename T>
class Myvector
{
private:
T * first;
T * last;
T * end;
};
要实现的函数:
构造函数 析构函数 拷贝构造 拷贝赋值 at() empty() full() front() back() size() clear() expand() 二倍扩容函数 push_back() pop_back()
头文件
#ifndef MYVECTOR_H
#define MYVECTOR_H
#include <iostream>
using namespace std;
template<typename T>
class Myvector
{
public:
Myvector():first(nullptr),last(nullptr),end(nullptr) {};
Myvector(Myvector const &other) ;
Myvector& operator=(Myvector const &other);
~Myvector();
T& at(int pos);
bool empty()const ;
bool full()const;
T& front();
T& back();
int size() const;
int capacity() const;
void clear();
void expand();
void push_back(const T &val);
void pop_back();
private:
T *first;
T *last;
T *end;
};
//拷贝构造
template<typename T>
Myvector<T>:: Myvector(Myvector const &other):
first(nullptr),last(nullptr),end(nullptr)
{
if(!other.empty())
return;
first = new T[other.size()];
last = first + other.size();
for(int i = 0; i < other.size(); i++)
{
*(first + i) = *(other.first + i);
}
}
//拷贝赋值
template <typename T>
Myvector<T>& Myvector<T>::operator=(const Myvector& other)
{
if (this == &other) {
return *this;
}
clear();
if (!other.empty()) {
first = new T[other.size()];
end = first + other.size();
last = first + other.size();
for (int i = 0; i < other.size(); ++i) {
*(first + i) = *(other.first + i);
}
}
return *this;
}
template<typename T>
Myvector<T>:: ~Myvector()
{
clear();
}
//at
template <typename T>
T& Myvector<T>::at(int index)
{
if (index < 0 || index >= size()) {
throw std::out_of_range("Invalid index!");
}
return *(first + index);
}
template <typename T>
bool Myvector<T>::empty() const {
return size() == 0;
}
template <typename T>
bool Myvector<T>::full() const
{
return size() == capacity();
}
// 返回首元素的引用
template <typename T>
T& Myvector<T>::front()
{
return *first;
}
// 返回尾元素的引用
template <typename T>
T& Myvector<T>::back()
{
return *(last - 1);
}
// 返回容器大小
template <typename T>
int Myvector<T>::size() const {
return last - first;
}
// 清空容器
template <typename T>
void Myvector<T>::clear() {
last = first;
}
template <typename T>
void Myvector<T>::expand()
{
int capacity =end-first;//计算当前容量
T *new_first=new T[capacity *2];//分配新的空间
T *new_last=new_first+size();//将原有数据偏移
//复制原有的数据
for(int i=0;i<size();++i)
{
first[i]=first[i];
}
delete[] first;//释放原有空间
first =new_first;
last =new_last;
end=first+capacity *2;
}
// 在末尾添加一个元素
template <typename T>
void Myvector<T>::push_back(const T& value) {
if (full())
expand();
*last = value;
last++;
}
// 移除末尾的元素
template <typename T>
void Myvector<T>::pop_back() {
if (empty())
return;
last--;
}
// 返回容器的容量
template <typename T>
int Myvector<T>:: capacity() const {
return end - first;
}
#endif // MYVECTOR_H
主函数文件
#include <iostream>
#include "myvector.h"
using namespace std;
int main()
{
Myvector<int> v1;
cout << "sizeof(v1) = " << sizeof(v1) << endl;
cout <<"empty="<< v1.empty() << endl;
for(int i = 0; i < 20; i++)
{
v1.push_back(i);
}
cout <<"empty="<< v1.empty() << endl;
cout <<"size="<< v1.size() << endl;
cout <<"full="<< v1.full() << endl;
v1.at(v1.size() - 1) = 40;
for(int i = 0; i < 20; i++)
{
cout << v1.at(i) << " ";
}
cout << endl;
cout <<"front="<< v1.front() << endl;
cout <<"back="<< v1.back() << endl;
v1.pop_back();
cout <<"size="<< v1.size() << endl;
v1.clear();
cout <<"empty="<< v1.empty() << endl;
cout <<"size="<< v1.size() << endl;
return 0;
}