首页 > 编程语言 >C++模板类-数组

C++模板类-数组

时间:2022-10-07 16:12:30浏览次数:51  
标签:const int unsigned C++ return 数组 inline Array 模板

/*Container.h  所有容器的基类
/*MemoryObject 内存申请基类 我使用TBB申请内存
*/
template<typename T>
	class Container :public MemoryObject
	{
	protected:
		T* C_New(unsigned int number) {
			if (!number) return nullptr;

			T* ptr = (T*)GetMemoryDispatcher().Allocate(number * sizeof(T), 0, true);
			if (!ptr) return nullptr;
			return ptr;
		}

		void C_Delete(T*& ptr, unsigned int number) {
			if (!ptr) return;

			if (number > 0)
			{
				if (ValueBase<T>::NeedsConstructor) {
					for (unsigned int i = 0; i < number; i++)
					{
						(ptr + i)->~T();
					}
				}
			}
			GetMemoryDispatcher().Deallocate((char*)ptr, 0, true);
			ptr = nullptr;
		}
			
	};
/*Array.h
*/
template<typename T>
	class Array :public Container<T>
	{
	public:
		Array(unsigned int length = DEFAULT_LENGTH);
		Array(const Array<T>& array);
		~Array();
		unsigned int GetLength() const;
		unsigned int GetCount() const;
		T* GetPtr() const;
		unsigned int AddValue(const T& element);
		void Sort();
		unsigned int IndexOf(const T& element);
		void Union(const Array<T> array);
		void Clear();
		void Destroy();

		T& operator[] (unsigned int i)const;
		void operator=(const Array<T>& array);
	private:
		const static unsigned int DEFAULT_LENGTH = 4;

	protected:
		T* m_Buffer;
		unsigned int m_Length;
		unsigned int m_Count;
	public:
		class ArrayIterator
		{
		public:
			ArrayIterator(T* node) :m_Node(node) {}
			ArrayIterator(const ArrayIterator& iterator) { m_Node = iterator.m_Node; }
			~ArrayIterator() = default;

			//运算符重载
			inline void operator= (const ArrayIterator& iterator) { m_Node = iterator.m_Node; }
			inline bool operator!=(const ArrayIterator& iterator) { return (m_Node != iterator.m_Node); }
			inline bool operator==(const ArrayIterator& iterator) { return (m_Node == iterator.m_Node); }
			inline ArrayIterator operator++() { m_Node++; return (*this); }
			inline ArrayIterator operator++(int) { ArrayIterator temp = *this;  ++* this;  return temp; }
			inline ArrayIterator operator--() { m_Node--; return (*this); }
			inline ArrayIterator operator--(int) { ArrayIterator temp = *this;  --* this;  return temp; }
			inline T& operator*() const { return *m_Node; };
			inline T* operator->() const { return m_Node; };
		protected:
			T* m_Node;
		};
		inline ArrayIterator Begin() const { return (ArrayIterator)m_Buffer; }
		inline ArrayIterator End() const { return (ArrayIterator(m_Buffer + GetLength())); }
	};
#include "Array.inl"
/*Array.inl
/*部分函数待更新  先够用就可以了
*/
#include "Array.h"
template<typename T>
inline Array<T>::Array(unsigned int length)
{
	MAC_ASSERT(length);
	m_Buffer = this->C_New(length);
	ZeroMemory(m_Buffer, sizeof(T) * length);
	m_Length = length;
	m_Count = 0;
}
template<typename T>
inline Array<T>::Array(const Array<T>& array)
{
	m_Buffer = nullptr;
	m_Length = 0;
	m_Count = 0;
	*this = array;
}
template<typename T>
inline Array<T>::~Array()
{
	this->C_Delete(m_Buffer, m_Length);
	m_Length = 0;
	m_Count = 0;
}
template<typename T>
inline unsigned int Array<T>::GetLength() const
{
	return m_Length;
}
template<typename T>
inline unsigned int Array<T>::GetCount() const
{
	return m_Count;
}
template<typename T>
inline T* Array<T>::GetPtr() const
{
	return m_Buffer;
}
template<typename T>
inline unsigned int Array<T>::AddValue(const T& element)
{
	if (m_Length >= m_Count) {
		S_NEW(m_Buffer + m_Count)T(element);
		m_Count++;
		return m_Count - 1;
	}
	return m_Length;
	//TODO  抛异常
}
template<typename T>
inline void Array<T>::Clear()
{
	if (m_Count == 0) return;
	if (ValueBase<T>::NeedsDestructor) {
		for (unsigned int i = 0; i < m_Count; i++)
		{
			m_Buffer[i].~T();
		}
	}
	m_Count = 0;
}
template<typename T>
inline void Unknown::Array<T>::Destroy()
{
	this->Clear();
	m_Count = 0;
	Delete(m_Buffer, m_Length);
	m_Length = 0;
}
template<typename T>
inline void Array<T>::Sort()
{

}
template<typename T>
inline unsigned int Array<T>::IndexOf(const T& element)
{
	for (unsigned int i = 0; i < m_Count; i++)
	{
		if (element == m_Buffer[i]) return i;
	}
	return m_Length-1; //返回最后一个数据索引
}
template<typename T>
inline void Array<T>::Union(const Array<T> array)
{

}
template<typename T>
inline T& Array<T>::operator[](unsigned int i) const
{
	MAC_ASSERT(i < m_Count);
	return m_Buffer[i];
}
template<typename T>
inline void Array<T>::operator=(const Array<T>& array)
{
	if (m_Length >= array.GetLength())
	{
		this->Clear();
		m_Count = array.GetCount();
		m_Length = array.GetLength();
		T* tPtr = array.GetPtr();
		for (unsigned int i = 0; i < m_Count; i++)
		{
			S_NEW(m_Buffer + i)T(tPtr[i]);
		}
	}
	else
	{
		this->Clear();
		m_Count = array.GetCount();
		m_Length = array.GetLength();
		T* tPtr = array.GetPtr();
		m_Buffer = this->C_New(m_Length);
		ZeroMemory(m_Buffer, sizeof(T) * m_Length);
		if (!m_Buffer) return;
		for (unsigned int i = 0; i <m_Count ; i++)
		{
			S_NEW(m_Buffer + i)T(tPtr[i]);
		}
	}

 

标签:const,int,unsigned,C++,return,数组,inline,Array,模板
From: https://www.cnblogs.com/zjr0/p/16759928.html

相关文章

  • C++模板基础知识
    源码编译环境:win10x86反汇编软件:IDAPro(胖大妈)第一次接触到模板是在C#的泛型编程,对其表面的理解是可以对一些约束范围内参数类型的方法进行重用,可以少写一些方法。在后......
  • 【模板】筛法
    \(prime\)constintN=1e7+10;intprime[N];boolnotprime[N];intminprime[N];voidprime_sieve(constintmaxn){ registerinti,multi_helper; registerin......
  • C++11特性(上)
    写在前面今天我们谈谈C++的一些语法,这些语法是C++11标准下新增的.有的人感觉学C++很难,那么C++11标准出来之后你会发现学习的成本又增加了.C++11增添了很多特性,有有用的,......
  • C++绘制玫瑰花
    C++绘制玫瑰花源码#include<stdio.h>#include<windows.h>#include<math.h>#include<graphics.h>#include<mmsystem.h>#pragmacomment(lib,"winmm.lib")//定......
  • C/C++基于朋友圈的商品推荐系统
    C/C++基于朋友圈的商品推荐系统基于朋友圈的商品推荐【课题内容和要求】模拟基于朋友圈的商品推荐,可以用于任何电商,如淘宝、京东等。假设目前用户有100人,每人均有自己......
  • 微信小程序排行榜页面模板
    1需求在开发一款背单词的微信小程序时,为了加强用户的体验感,刺激用户积极学习,小程序中需要有排行榜的模块。通过打开天数来排名,让用户有攀比学习的心里。具体的页面截图如......
  • 模板基类与正确的派生类函数调用--Effective C++ Item 43
    问题描述假设我们有这样一个业务场景,我们管理着许多公司,每个公司都有一个自己的许多日志信息需要处理,于是为了方便,我们写了一个模板类用来处理这些公司的信息,并且将这些公......
  • 189. 轮转数组
    189.轮转数组给你一个数组,将数组中的元素向右轮转k 个位置,其中 k 是非负数。 示例1:输入:nums=[1,2,3,4,5,6,7],k=3输出:[5,6,7,1,2,3,4]解释:向右轮......
  • C/C++语言 MD5例子
    之前研究了一下在C中进行MD5加密,由于找了很久没有找到现成的库文件,所以所幸自己去写了一下。个人感觉C的便捷性没有Python好的原因就是这里。下面是我写的一个例子。mian......
  • 数组的上半比分操作的优化
    对于一个二维数组进行上半部分操作求和求avg#include<cstdio>#include<iostream>#include<cmath>usingnamespacestd;constintN=1000;doublea[N][N];int......