第一次手写vector

15 篇文章 0 订阅
14 篇文章 0 订阅

内容 

#ifndef VECTOR_H_
#define VECTOR_H_

#include<cstdlib>

namespace VECTOR
{
	template<typename Ty>
	class Vector
	{
	public:
		//构造,析构
		explicit Vector(const int def = 0);
		Vector(const Vector&);
		explicit Vector(const int, const Ty&);
		~Vector();
		//存取
		void push_back(const Ty&);
		void pop_back();
		//索引
		Ty& operator[](const int);
		const Ty& operator[](const int)const;
		//查找,返回索引,没有返回-1
		const int find(const Ty&)const;
		//返回当前占用
		const int size(void)const;
		//定义容量
		void resize(const int);
	private:
		//把b中的前size个元素拷贝到a
		static void Copy(Ty* a,const int size, const Ty* b);
		//容量提升处理
		const int upcapacity(const int size);
		//判断是否为空
		const bool empty();
		//判断是否满
		const bool full();
		//扩容
		void change(const int);

		Ty* m_value;//储存器
		int m_size;//当前占用
		int m_capacity;//容量
	};
}
#endif // !Vector_H_

实现

namespace VECTOR
{
	//创建def容量
	template<typename Ty>
	 VECTOR::Vector<Ty>::Vector(const int def)
	{
		this->m_size = 0;
		this->m_value = new Ty[this->upcapacity(def)];
	}
	//创建n个t的容器
	template<typename Ty>
	VECTOR::Vector<Ty>::Vector(const int n, const Ty& t)
	{
		this->m_size = n;
		this->m_value = new Ty[this->upcapacity(n)];
		for (int i = 0; i < this->m_size; ++i)
			(this->m_value)[i] = t;
	}
	//拷贝构造,不用容量提升
	template<typename Ty>
	VECTOR::Vector<Ty>::Vector(const Vector<Ty>& ve)
	{
		this->m_size = ve.m_size;
		this->m_capacity = ve.m_capacity;
		this->m_value = new Ty[this->m_capacity];
		Copy(this->m_value,ve.m_size, ve.m_value);
	}
	//析构
	template<typename Ty>
	 VECTOR::Vector<Ty>::~Vector()
	{
		this->m_capacity = 0;
		this->m_size = 0;
		delete[] (this->m_value);
		this->m_value = nullptr;
	}
	//尾插
	template<typename Ty>
	 void VECTOR::Vector<Ty>::push_back(const Ty& t)
	{
		 if (this->full())
			 this->change(this->upcapacity(this->m_capacity));
		(this->m_value)[(this->m_size)++] = t;
	}
	//尾删
	template<typename Ty>
	 void VECTOR::Vector<Ty>::pop_back()
	{
		if (this->empty())
			exit(EXIT_FAILURE);
		--(this->m_size);
	}
	//索引
	template<typename Ty>
	 Ty& VECTOR::Vector<Ty>::operator[](const int n)
	{
		if (n <= 0 || n > this->m_size)
			exit(EXIT_FAILURE);
		return (this->m_value)[n];
	}
	 template<typename Ty>
	 const Ty& VECTOR::Vector<Ty>::operator[](const int n) const
	 {
		 if (n <= 0 || n > this->m_size)
			 exit(EXIT_FAILURE);
		 return (this->m_value)[n];
	 }
	//查找
	template<typename Ty>
	const int VECTOR::Vector<Ty>::find(const Ty& t) const
	{
		for (int i = 0; i < this->m_size; ++i)
			if ((this->m_value)[i] == t)
				return i;
		return -1;
	}
	//返回占用
	template<typename Ty>
	 const int VECTOR::Vector<Ty>::size(void) const
	{
		return this->m_size;
	}
	//定义容量
	template<typename Ty>
	 void VECTOR::Vector<Ty>::resize(const int n)
	{
		 if (n <= 0)
			 exit(EXIT_FAILURE);
		 if (n < this->m_size)
			 this->m_size = n;
		 this->m_capacity = n;
		 this->change(n);
	}
	//拷贝
	template<typename Ty>
	void VECTOR::Vector<Ty>::Copy( Ty* a ,const int size, const Ty* b)
	{
		for (int i = 0; i < size; ++i)
			a[i] = b[i];
	}
	//空
	template<typename Ty>
	 const bool VECTOR::Vector<Ty>::empty()
	{
		if (this->m_size == 0)
			return true;
		return false;
	}
	//满
	template<typename Ty>
	 const bool VECTOR::Vector<Ty>::full()
	{
		if (this->m_size == this->m_capacity)
			return true;
		return false;
	}
	//容量抬升
	template<typename Ty>
	 const int VECTOR::Vector<Ty>::upcapacity(const int size)
	{
		return this->m_capacity = size * 2 + 5;
	}
	//变容
	template<typename Ty>
	void VECTOR::Vector<Ty>::change(const int capacity)
	{
		Ty* temp = new Ty[capacity];
		if (this->m_capacity < capacity)
			Copy(temp, this->m_size, this->m_value);
		else
			Copy(temp, capacity, this->m_value);
		delete[]this->m_value;
		this->m_value = temp;
	}
}

以下是一个使用触控板实现手写输入的 Unity 示例代码,可以在 Unity 中创建一个新的场景,添加一个 Plane 对象作为背景,然后将以下代码添加到一个新的 C# 脚本中: ```csharp using UnityEngine; public class HandwritingInput : MonoBehaviour { private Vector2 lastPosition; private Vector2 currentPosition; void Update() { // 获取触摸输入 if (Input.touchCount > 0) { Touch touch = Input.GetTouch(0); // 第一次触摸屏幕 if (touch.phase == TouchPhase.Began) { lastPosition = touch.position; currentPosition = touch.position; } // 移动手指 else if (touch.phase == TouchPhase.Moved) { lastPosition = currentPosition; currentPosition = touch.position; // 计算手指移动方向和距离 Vector2 direction = (currentPosition - lastPosition).normalized; float distance = Vector2.Distance(currentPosition, lastPosition); // 如果手指移动距离太短,则不进行绘制 if (distance > 10) { // 在当前位置绘制一个小圆点 GameObject dot = GameObject.CreatePrimitive(PrimitiveType.Sphere); dot.transform.localScale = Vector3.one * 0.1f; dot.transform.position = new Vector3(currentPosition.x, currentPosition.y, 0); } } } } } ``` 这个示例中,我们使用触摸输入获取手指的位置,然后在当前位置绘制一个小圆点。如果手指移动距离太短,则不进行绘制。 当然,这只是一个简单的示例,实际上手写输入的实现要比这个复杂得多。需要使用更高级的算法来识别手写字母或数字,以及考虑到用户体验和识别准确性等方面的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值