动态二维数组

/*

 cgl_array2d.hpp

 sdragon 2006-10-08 22:26:44

 创建二维数组的程序。array2d<T>::array_t是C的纯指针模式,一定要使用
   delete_array2d<T>()删除。
 由vector<T>创建的数组的效率要高一些

*/
#ifndef CGL_ARRAY2D_HPP_20061008222644
#define CGL_ARRAY2D_HPP_20061008222644

#include <vector>

namespace cgl{

/*C风格的二维数组*/
template<typename T>
void new_array2d(T** &v, int size_x, int size_y)
{
	v = new T*[size_y];
	for(int y=0; y < size_y; ++y)
		v[y] = new T[size_x];
}

template<typename T>
void new_array2d(T** &v, int size_x, int size_y, const T& val)
{
	v = new T*[size_y];
	for(int y=0; y < size_y; ++y)
	{
		v[y] = new T[size_x];
		for(int x=0; x < size_x; ++x)
			v[y][x] = val;
	}
}

template<typename T>
void delete_array2d(T** &v, int size_y)
{
	for(int y=0; y < size_y; ++y)
		delete[] v[y];
	delete[] v;
	v = NULL;
}

/*vector<T>风格的动态数组*/
template<typename T>
void new_array2d(std::vector<std::vector<T> > &v, int size_x, int size_y)
{
	v.resize(size_y);
	for(int y=0; y < size_y; ++y){
		v[y].resize(size_x);
	}

	//下面是两种效率低下的初始化方法,这些都是c++的“坑”
	//方案2
	//速度差不多,当size_y增大的时候,这种方案速度会越来越慢
	//这个会产生一个vector<y>的临时变量,并对每一次x初始化进行复制
	//v.resize(size_x, std::vector<T>(size_y));
	//v.swap( std::vector<std::vector<T> >(size_x, std::vector<T>(size_y)) );
	//方案3
	//直接符值的速度非常慢,直接初始化的速度也是非常慢
	//这个会产生一个整个数组的副本,然后赋值给变量
	//v = std::vector<std::vector<T> >(size_x, std::vector<T>(size_y))
}

template<typename T>
void new_array2d(std::vector<std::vector<T> > &v, int size_x, int size_y, const T& val)
{
	v.resize(size_y);
	for(int y=0; y < size_y; ++y)
		v[y].resize(size_x, val);
}

template<typename T>
void delete_array2d(std::vector<std::vector<T> > &v)
{
	std::vector<std::vector<T> > tmp;
	v.swap(tmp);
}

/*
** vector<T>的二维数组
*/

template<typename T>
class array2d
{
public:
	typedef T value_type;
	typedef std::vector<T> array_x;
	typedef std::vector<array_x> array_y;
private:
	array_y m_array;
public:
	array2d():m_array(){/*void*/}
	array2d(const array2d& v):m_array(v){/*void*/}
	array2d(size_t x, size_t y)
	{
		this->resize(x, y);
	}
	array2d(size_t x, size_t y, const T& value)
	{
		this->resize(x, y, value);
	}

	bool empty()const
	{
		return m_array.empty();
	}

	void clear()
	{
		delete_array2d<T>(m_array);
	}

	array_x& operator[](int y)
	{
		return m_array[y];
	}

	const array_x& operator[](int y)const
	{
		return m_array[y];
	}

	array2d<T>& operator=(const array2d<T>& v)
	{
		m_array = v.m_array;
		return *this;
	}

	size_t size()const
	{
		return size_x() * size_y();
	}

	size_t size_x()const
	{
		return empty() ? 0 : m_array[0].size();
	}

	size_t size_y()const
	{
		return m_array.size();
	}

	void resize(size_t x, size_t y)
	{
		new_array2d<T>(m_array, x, y);
	}

	void resize(size_t x, size_t y, const value_type& value)
	{
		new_array2d<T>(m_array, x, y, value);
	}

	void swap(array2d<T>& v)
	{
		m_array.swap(v.m_array);
	}
};

}// end namespace cgl

#endif //CGL_ARRAY2D_HPP_20061008222644

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值