vector
(O(1)头尾删,O(1)头插,有大部分STL的函数,暂不支持随机删除插入(谁用向量干这个))
#ifndef MYVECTOR
#define MYVECTOR
#include<bits/stdc++.h>
using namespace std;
template<typename T>
class myvector
{
T* data;
int begin;
int end;
int length,max_len=32,_max_len;
void to_big(){
int tlen=max_len*1.618+0.555;
T* newdata;
if(tlen<=_max_len){
newdata=new T[tlen];
max_len=tlen;
}else{
newdata=new T[_max_len];
max_len=_max_len;
}
int j=0;
for(int i=begin;i<end;i++,j++){
newdata[j]=data[i];
}
begin=0;
end=j;
delete data;
data=newdata;
}
void to_begin(){
T* newdata;
newdata=new T[max_len];
int j=0;
for(int i=begin;i<end;i++,j++){
newdata[j]=data[i];
}
begin=0;
end=j;
delete data;
data=newdata;
}
public:
myvector(int Max_len=100000000):_max_len(Max_len){
data = new T[max_len];
begin = 0;
end = 0;
length =0;
}
~myvector(){
delete data;
}
void clear(){
end=length=begin=0;
return;
}
//入队:push
void push_back(T tmp)
{
if(length == _max_len)
{
if(max_len!=_max_len){
to_big();
}else return;
}
if(end==max_len){
if(max_len==_max_len){
if(begin>0)
end=0;
else return;
}else{
int have_len=end-begin;
if((double)have_len/max_len<0.618){
to_begin();
}else to_big();
}
}
data[end] = tmp;
end++;
length++;
return;
}
//出队:pop
T pop_front()
{
if(length == 0)
{
return 0;
}
begin++;
length--;
T re= data[begin-1];
if(begin>=max_len-1){
if(max_len==_max_len){
begin=0;
}else{
to_big();
}
}
return re;
}
T pop_back()
{
if(length == 0)
{
return 0;
}
end--;
length--;
T re= data[end];
if(end<0){
end=max_len+end;
}
return re;
}
//队头:front
T& front()
{
return data[begin];
}
T& operator[](int t){
return data[(begin+t)%max_len];
}
T& operator[](long long t){
return data[(begin+t)%max_len];
}
T& operator[](long t){
return data[(begin+t)%max_len];
}
//对大小:size
int size()
{
return length;
}
bool empty(){
return length==0;
}
};
#endif
queue
(内容包含但不局限于STL,全部O(1)(扩张为什么O(1)自己上网查))
#ifndef MYQUEUE
#define MYQUEUE
#include<bits/stdc++.h>
using namespace std;
template<typename T>
class myqueue
{
T* data;
int begin;
int end;
int length,max_len=32,_max_len;
void to_big(){
int tlen=max_len*1.618+0.555;
T* newdata;
if(tlen<=_max_len){
newdata=new T[tlen];
max_len=tlen;
}else{
newdata=new T[_max_len];
max_len=_max_len;
}
int j=0;
for(int i=begin;i<end;i++,j++){
newdata[j]=data[i];
}
begin=0;
end=j;
delete data;
data=newdata;
}
void to_begin(){
T* newdata;
newdata=new T[max_len];
int j=0;
for(int i=begin;i<end;i++,j++){
newdata[j]=data[i];
}
begin=0;
end=j;
delete data;
data=newdata;
}
public:
myqueue(int Max_len=100000000):_max_len(Max_len){
data = new T[max_len];
begin = 0;
end = 0;
length =0;
}
~myqueue(){
delete data;
}
void clear(){
end=length=begin=0;
return;
}
//入队:push
void push(T tmp)
{
if(length == _max_len)
{
if(max_len!=_max_len){
to_big();
}else return;
}
if(end==max_len){
if(max_len==_max_len){
if(begin>0)
end=0;
else return;
}else{
int have_len=end-begin;
if((double)have_len/max_len<0.618){
to_begin();
}else to_big();
}
}
data[end] = tmp;
end++;
length++;
return;
}
//出队:pop
T pop()
{
if(length == 0)
{
return 0;
}
begin++;
length--;
T re= data[begin-1];
if(begin>=max_len-1){
if(max_len==_max_len){
begin=0;
}else{
to_big();
}
}
return re;
}
//队头:front
T& front()
{
return data[begin];
}
T& operator[](int t){
return data[(begin+t)%max_len];
}
T& operator[](long long t){
return data[(begin+t)%max_len];
}
T& operator[](long t){
return data[(begin+t)%max_len];
}
//对大小:size
int size()
{
return length;
}
bool empty(){
return length==0;
}
};
#endif