1.
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
template<typename Type>
class my_vector
{
protected:
Type* begin;
Type* end;
Type* last;
public:
//无参构造
my_vector()
{
end = nullptr;
last = nullptr;
begin = nullptr;
}
//有参构造
my_vector(int size,Type a);
//拷贝构造
my_vector(const my_vector &s);
//析构
~my_vector();
//写入尾插
void push_back(Type d);
//尾删
void pop_back()
{
if(end == begin)
{
throw -2;
}
else
{
--end;
}
}
Type show(int a);
//数据实际长度
int size_lenth()const;
//数据总长度
int type_lenth()const
{
int s = (last - begin)+1;
return s;
}
//清空
void clear();
//头结点的使用
Type Begin();
//栈判空
bool empty();
Type at(int a);
//二倍扩容
void expand();
//拷贝赋值
my_vector &operator = (const my_vector &other);
};
int main()
{
my_vector<int> s(3,6);
my_vector<int> s2;
for(int i = 0;i<4;i++)
{
try
{
cout<<s.show(i)<<endl;
}
catch(int b)
{
cout<<"已经到队尾"<<endl;
break;
}
}
int a = 6;
my_vector<int> s1(s);
s1.push_back(a);
cout<<s1.type_lenth()<<endl;
s1.push_back(5);
s1.push_back(4);
s1.push_back(3);
s1.push_back(3);
//栈判空
//cout<<boolalpha<<s1.empty()<<endl;
//cout<<"清空s1"<<endl;
//s1.clear();
cout<<boolalpha<<s1.empty()<<endl;
//cout<<__LINE__<<endl;
for(int i = 0; i<20;i++)
{
try
{
cout<<s1.show(i)<<" size = "<<s1.size_lenth()<<" sizeof ="<<s1.type_lenth()<<endl;
}
catch(int b)
{
cout<<"已经到队尾"<<endl;
break;
}
}
for(int i = 0; i<20;i++)
{
try
{
cout<<s1.show(i)<<" size = "<<s1.size_lenth()<<" sizeof ="<<s1.type_lenth()<<endl;
}
catch(int b)
{
cout<<"已经到队尾"<<endl;
break;
}
}
s2.push_back(6);
s2.push_back(6);
s2.push_back(6);
s2.push_back(6);
s2.push_back(6);
s2.push_back(6);
for(int i = 0; i<20;i++)
{
try
{
cout<<s2.show(i)<<" size = "<<s2.size_lenth()<<" sizeof ="<<s2.type_lenth()<<endl;
}
catch(int b)
{
cout<<"已经到队尾"<<endl;
break;
}
}
s = s1;
for(int i = 0; i<20;i++)
{
try
{
cout<<s.show(i)<<" size = "<<s.size_lenth()<<" sizeof ="<<s.type_lenth()<<endl;
}
catch(int b)
{
cout<<"已经到队尾"<<endl;
break;
}
}
return 0;
}
//有参构造
template<typename Type>
my_vector<Type>::my_vector(int size, Type a)
{
begin = new Type[size];
for(int i = 0; i < size;i++)
{
*(begin+i) = a;
}
end = begin+size;
last = (begin+size);
}
//拷贝构造
template<typename Type>
my_vector<Type>::my_vector(const my_vector &s)
{
//目标是空指针时本指针也需要为空指针
if(s.begin == nullptr &&s.begin == s.end)
{
this->begin = nullptr;
this->last = nullptr;
this->end = nullptr;
}
else if(s.begin == s.end)
{
//如果是有指针空间且没有内容
int sz = s.type_lenth();
this->begin = new Type[sz];
memset(this->begin,0,sizeof(Type)*sz);
this->end = this->begin;
this->last = this->begin + sz;
}
else
{
//既有空间也有内容
int sz = s.type_lenth();
this->begin = new Type[sz];
memcpy(this->begin,s.begin,sizeof(Type)*sz);
this->end = this->begin + s.size_lenth();
this->last = this->begin + sz;
}
}
//析构
template<typename Type>
my_vector<Type>::~my_vector()
{
//如果此时都是空指针就直接退出
//不是空指针就删除begin的空间
if(nullptr == begin&&nullptr == last)
{
//cout<<"析构"<<endl;
}
else
{
delete []begin;
begin = nullptr;
last = nullptr;
end = nullptr;
//cout<<"析构"<<endl;
}
}
//写入尾插
template<typename Type>
void my_vector<Type>::push_back(Type d)
{
//cout<<__LINE__<<endl;
//判断是否是空指针
//如果是给一个空间
if(nullptr == begin)
{
begin = new Type[2];
*begin = d;
end = begin;
last = begin+1;
}
else
{
if(end == last)
{
//如果空间不够就直接二倍扩容
int size = last-begin+1;
Type* ptr = new Type[size+1];
memcpy(ptr,begin,sizeof(Type)*size);
delete []begin;
begin = nullptr;
last = nullptr;
end = nullptr;
begin = new Type[size*2];
memcpy(begin,ptr,sizeof(Type)*size);
delete []ptr;
end = begin+size-1;
last = begin+size*2-1;
//this->expand();
}
}
*(end) = d;
end++;
}
//固定位置的展示
template<typename Type>
Type my_vector<Type>::show(int a)
{
if(end == begin)
{
throw -3;
}
if(a >= (end - begin))
{
throw -1;
}
else
{
//cout<<(end)<<endl;
return *(begin+a);
}
}
//数据总长度
template<typename Type>
int my_vector<Type>::size_lenth() const
{
if(end == begin)
{
throw -4;
}
else
{
int s = (end - begin);
return s;
}
}
//清空
template<typename Type>
void my_vector<Type>::clear()
{
if(end == begin)
{
throw -3;
}
else
{
end = begin;
}
}
//头结点的使用
template<typename Type>
Type my_vector<Type>::Begin()
{
if(begin == end)
{
throw -5;
}
else if(begin == nullptr)
{
throw -5;
}
else
{
return *begin;
}
}
//栈判空
template<typename Type>
bool my_vector<Type>::empty()
{
if(end == begin)
{
return 1;
}
else
{
return 0;
}
}
//固定位置
template<typename Type>
Type my_vector<Type>::at(int a)
{
if(a > (end - begin))
{
//at的不安全错误
throw -6;
}
else
{
return *(begin + a);
}
}
//二倍扩容
template<typename Type>
void my_vector<Type>::expand()
{
//如果此指针是空指针就给一个字节
if(nullptr == begin)
{
begin = new Type[1];
end = begin;
last = begin;
}
else
{
//其他的
if(end == last)
{
//二倍扩容
int size_l = last-begin;
int size_e = end - begin;
//中间指针
Type* ptr = new Type[size_e+1];
//拷贝到中间指针中
memcpy(ptr,begin,sizeof(Type)*size_l);
//删除begin指针
delete []begin;
begin = nullptr;
last = nullptr;
end = nullptr;
//重构begin指针
begin = new Type[size_l*2];
//拷贝回来
memcpy(begin,ptr,sizeof(Type)*size_l);
delete []ptr;
//修改end和last指向
end = begin+size_e;
last = begin+size_l*2;
}
}
}
//拷贝赋值
template<typename Type>
my_vector<Type> &my_vector<Type> ::operator =(const my_vector &other)
{
int size_l = other.last-other.begin+1;
int size_e = other.end - other.begin;
//没有空间、没有内存
if(other.end == other.begin&&begin == nullptr)
{
this->begin = nullptr;
this->last = nullptr;
this->end = nullptr;
}//有空间、没内存
else if(other.end == other.begin)
{
delete []this->begin;
this->begin = new Type[size_l];
this->end =this->begin;
this->last = this->begin+size_l;
}
else
{
//既有空间也有内存
delete []this->begin;
this->begin = new Type[size_l];
memset(this->begin,0,sizeof(Type)*size_l);
memcpy(this->begin,other.begin,sizeof(Type)*size_l);
this->end = this->begin + size_e;
this->last = this->begin + size_l-1;
}
return *this;
}
#工程引导文件,工程管理文件
QT += core gui
#需要加入的类库 core核心库 gui图形界面类库
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#超过4版本后要自动加widgets类库,前面的版本中,widgets被合并在core类库中
CONFIG += c++11
#支持C++11以后的版本
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
#源文件
SOURCES += \
main.cpp \#主函数
widget.cpp#包含源文件
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
//头文件
#ifndef WIDGET_H
#define WIDGET_H
//防止头文件重复包含
#include <QWidget>
//引入头文件
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; } //声明命名空间
QT_END_NAMESPACE
//自定义的界面类,公共继承自QWidget
class Widget : public QWidget
{
Q_OBJECT
//处理信号与槽函数
public:
Widget(QWidget *parent = nullptr);//构造函数声明
~Widget(); //析构函数声明
private:
Ui::Widget *ui;
//使用ui界面类定义出来的指针,用于找到ui界面中拖拽出来的组件
};
#endif // WIDGET_H
//源文件
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent) //构造函数的定义
: QWidget(parent) //调用父类有参构造
, ui(new Ui::Widget) //给ui指针申请空间
{
//ui的指针
ui->setupUi(this); //调用ui界面函数,给拖拽的组件申请空间并设置相关数据
}
//析构函数的定义
Widget::~Widget()
{
delete ui;
}
//主函数
#include "widget.h" //包含自定义的头文件
#include <QApplication>
//主函数
int main(int argc, char *argv[])
{
QApplication a(argc, argv);//使用应用程序实例化对象,调用有参构造
//使用自定义类实例化一个对象,调用无参构造
Widget w;
//调用show()函数
w.show();
//轮询等待 等待用户操作、等待事件处理、等待相关信号发射
return a.exec();
}