widget.cpp
#include <iostream>
using namespace std;
//使用模板类
template<typename T>
class myvector
{
private:
T *first;
T *last;
T *end;
public:
//无参构造
myvector()
{
first = last = end = nullptr;
cout<< "无参构造"<<endl;
}
//有参构造
myvector(int n, const T &val)
{
first = new T[n];
for(int i = 0; i<n; i++)
{
first[i] = val;
}
last = first + n;
end = first + n;
cout<<"有参构造"<<endl;
}
//拷贝构造函数
myvector(const myvector &other):first(new T(*(other.first))),last(new T(*(other.last))), end(new T(*(other.end)))
{
cout<<"拷贝构造函数"<<endl;
}
//拷贝赋值函数
myvector &operator=(const myvector &other)
{
int len = other.last-other.first;
int size = other.end-other.first;
first = new T[size];
for(int i =0; i<len; i++)
{
first[i] = other.first[i];
}
last = first + len;
end = first +size;
cout<<"拷贝赋值函数"<<endl;
return *this;
}
//析构函数
~myvector()
{
delete []first;
first = nullptr;
last = nullptr;
end = nullptr;
cout<<"析构函数"<<endl;
}
//at函数
T &at(int index)
{
//判断index是否在范围内
if(index<0 && index>last-first)
{
cout<<"下标超出范围"<<endl;
throw(-1);
}
else
{
return first[index];
}
}
//判空函数
bool empty()
{
return first == last;
}
//判满函数
bool full()
{
return last == end;
}
//返回第一个元素的函数
T front()
{
return *first;
}
//返回最后一个元素的函数
T back()
{
return *(last-1);
}
//返回表中元素个数的函数
int size()
{
return (last-first);
}
//二倍扩容函数
void expand()
{
int len = end-first;
T *temp = new T(2*len);
memcpy(temp, first, sizeof(T)*len);
delete []first;
first = temp;
last = first+len;
end = first + 2*len;
}
//在末尾添加一个元素
void push_back(const T e)
{
//判满
if(full()==true)
{
cout<<"链表已满,添加失败"<<endl;
cout<<"二倍扩容"<<endl;
expand();
}
*last = e;
last++;
cout<<"插入成功"<<endl;
}
//删除最后一个元素函数
void pop_back()
{
//判空
if(empty()==true)
{
cout<<"链表为空,删除失败"<<endl;
}
last--;
cout<<"删除成功"<<endl;
}
//删除所有元素函数
void clear()
{
while(last != first)
{
// pop_back()函数删除当前最末的一个元素
pop_back();
}
}
};
int main()
{
myvector<char> c1(2,'a');
myvector<char> c2(c1);
myvector<char> c3;
c3 = c1;
c1.push_back('b');
c1.push_back('c');
c1.push_back('d');
cout<<c1.at(1)<<endl;
cout<<c1.at(3)<<endl;
cout<<c1.at(4)<<endl;
cout<<"表中元素个数为:"<<c1.size()<<endl;
cout<<"第一个元素为:"<<c1.front()<<endl;
cout<<"最后一个元素为:"<<c1.back()<<endl;
//调用在末尾删除函数
c1.pop_back();
cout<<"表中元素个数为:"<<c1.size()<<endl;
//清空
c1.clear();
cout<<"表中元素个数为:"<<c1.size()<<endl;
return 0;
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QDebug>
#include<QMessageBox>
#include<QMovie>
#include "second.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
signals:
void btn1_signal();
public slots:
void btn1_slot();
void btn2_slot();
private:
Ui::Widget *ui;
QPushButton *btn1;
QPushButton *btn2;
QLineEdit *edit1;
QLineEdit *edit2;
QLabel *lab1;
QLabel *lab2;
QLabel *lab3;
};
#endif // WIDGET_H
main.c
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
second.cpp
#include "second.h"
#include "ui_second.h"
second::second(QWidget *parent) :
QWidget(parent),
ui(new Ui::second)
{
ui->setupUi(this);
//窗口属性
this->setFixedSize(480,360);
this->setWindowTitle("网络聊天室");
}
second::~second()
{
delete ui;
}
void second::btn1_jumpslot()
{
this->show();
}
second.h
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
namespace Ui {
class second;
}
class second : public QWidget
{
Q_OBJECT
public:
explicit second(QWidget *parent = nullptr);
~second();
private:
Ui::second *ui;
public slots:
void btn1_jumpslot();
};
#endif // SECOND_H