STL-数组模板

#pragma once
#include<iostream>
#include<initializer_list>
using namespace std;



template <class T,int size>
class myarray
{

public:

	friend istream & operator >>(istream &, T & my);

public:
	myarray();
	myarray(initializer_list<T>);
	myarray(const myarray & mycopy) = delete;
	~myarray();
	T  operator [](int n);

	
public:
	int empty(); //判断是否为空  1 空 0 非空
	int full();  //判断是否为满  1 满 0 非满
	int mysize();  //返回数组大小(元素个数)  
	void myassign(const int & _val); //初始化为_Val
	void myarraycat(int _val);  //给数组追加内存  _VAL 需增加的大小

	
/* 增*/
	void myInsert(T key, T _val,char chr);  //key 关键字,要插入的值 ,chr 前后插 f 前插, r 后插  
	void mypush_back(T key);  //尾部插入一个元素  key 要插入的元素

/*删*/
	void delAll();  //清空
	void mydel(int _index); //删除某一元素  _index 下标

/*查*/
	T myat(const int & _index);  //查询下标为_index里的元素,返回为该元素   
	int mysrea(T _val);  //检测某_val在数组中是否存在,返回值为下标 ,-1表示不存在

/*改*/
	void myswap(const myarray & s);  //两数组交换
	void myexchange(int _index, T & _val); //改变某一元素的值


public:
	int sn;  //数组实际元素个数	
	
private:
	int n; //数组大小
	T *p;  //数组源
	
};

template <class T, int size>
int myarray<T, size>::empty()  //判断是否为空
{
	if (this->sn == 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

template <class T, int size>
int  myarray<T, size>::full() //判断是否为满
{
	if (this->sn == this->n)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

template <class T, int size>
void myarray<T, size>::mypush_back(T key)  //尾部插入
{
	if (this->sn == this->n)  //数组已满
	{
		cout << "已满,无法插入" << endl;
	}

	else
	{
		this->p[this->sn] = key;
	}
}


template <class T, int size>
void myarray<T, size>::myInsert(T key, T _val,char chr)  //随机插入
{
	if (this->sn == this->n)
	{
		cout << "数组已满,无法插入";
	}
	else
	{
		int indx = mysrea(key);
		
		if (indx != -1)
		{
			if (chr == 'f')
			{
				for (int i = sn; i > indx; i--) //前插
				{
					this->p[i] = this->p[i - 1];
				}

				this->p[indx] = _val;
				sn += 1;
			}
			else
			{
				for (int i = sn; i > indx+1; i--)  //后插
				{
					this->p[i] = this->p[i - 1];
				}

				this->p[indx+1] = _val;
				sn += 1;
			}
		}

		else
		{
			cout << "没有要查找的元素关键字" << endl;
		}
	}

}

template <class T, int size>
void myarray<T, size>::delAll()  //清空
{
	free(this->p);
	sn = 0;
	n = 0;
}
template <class T, int size>
int myarray<T, size>::mysrea(T _Val)   //查询
{
	for (int i = 0; i < sn; i++)
	{
		if (this->p[i] == _Val)
		{
			return i;
		}
	}

	return -1;
}

template <class T, int size>
void  myarray<T, size>::myarraycat(int _val)  //给数组追加内存
{
	T *ptmp = new T[this->n + _val];

	for (int i = 0; i < this->sn; i++)
	{
		ptmp[i] = this->p[i];
	}
	free(this->p);
	this->n += _val;
	this->p = ptmp;
}

template <class T, int size>
void myarray<T, size>::mydel(int _index) //删除某一元素 ,_index下标
{
	for (int i = _index; i < (this->n)-1; i++)
	{
		this->p[i] = this->p[i + 1];
	}

	sn -= 1;   //实际元素个数
}



template <class T, int size>
void myarray<T, size>::myexchange(int _index,T & _val)  //改变某一元素 ,_index 下标,_val 要修改的值
{
	this->p[_index] = _val;
}



template <class T, int size>
myarray<T, size>::myarray() :p(nullptr), n(0)  //数组为空
{
}


template <class T, int size>
void myarray<T, size>::myswap(const myarray & s)  //数组相互交换
{
	if (s.n > this->n)
	{
		cout << "越界";
	}
	else
	{
		for (int i = 0; i < s.n; i++)
		{
			T tmp = this->p[i];
			p[i] = s.p[i];
			s.p[i] = tmp;
		}
	}
}


template <class T, int size>
int myarray<T, size>::mysize()   //返回元素个数
{
	return this->n;
}



template <class T, int size>
T myarray<T, size>::myat(const int & _index)  //返回指定下标元素
{
	return this->p[_index];
}



template <class T, int size>
void myarray<T,size>::myassign(const int & _val)  //赋值
{
	for (int i = 0; i < this->n; i++)
	{
		this->p[i] = _val;
	}
}


template <class T, int size>
T  myarray<T,size>::operator [](int n)  //重载[]括号
{
	return this->p[n];

}



template <class T, int size>
myarray<T, size>::myarray(initializer_list<T> mya) :n(size)
{
	sn = mya.size();
	p = new T[n]{0};
	int j = 0;
	for (auto i : mya)
	{
		p[j++] = i;
	}
}



template <class T,int size>
myarray<T,size>::~myarray()
{
	free(p);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值