C++向量Vector模板的实现

在项目中需要使用向量的时候,可以使用C++自带的vector.h来实现向量的存储,也可以自己实现向量的类。。

下面给出自己定义的向量类,需要什么函数都可以自己定义,是不是来的很方便呢。。。。:

/*********************************************************************/
/*                                                                   */
/*  Xiaoding: Vector C++ Library                                     */
/*                                                                   */
/*                                                                   */
/*   Poject Speech                                                   */
/*   Date:2012.03                                                    */
/*   E-mail: mdd_133@163.com                                         */
/*                                                                   */
/*********************************************************************/
#ifndef   _VECTOR_H
#define   _VECTOR_H  //防止头文件重复定义
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#namespace Xiaoding
{
   template <class Type>
   class Vector
   {
   public:
	    explicit Vector(void); //默认构造函数
		explicit Vector(int );//向量大小构造函数 防止通过隐式转换来调用这个构造函数
		explicit Vector(int, const Type&);//初始化大小,并赋初值
	    Vector(const Vector&); //复制构造函数
		virtual ~Vector(void); //析构函数
		//操作符重载函数
		Vector& operator = (const Vector&);
        inline Type& operator [] (int);
        //inline const Type& operator [] (int) const;
		
	friend std::ostream& operator<<(std::ostream& out, const Vector<Type>& V); //输出运算符重载
	//算术操作符重载
   Vector<Type> operator + (const Type&) const;
   Vector<Type> operator + (const Vector<Type>&) const;

   inline int get_size(void) const;
   private: 

   int size; //向量大小
   bool display; //是否显示标志
   /// 指向数据的指针
   Type *data; 

   };

  //创建一个大小为0的向量
  template <class Type>
Vector<Type>::Vector(void)
{
   size = 0;
   display = true;
   data = NULL;
}

/// @param new_size 向量的大小
template <class Type>
Vector<Type>::Vector(int new_size)
{
   // (if debug)      

   #ifdef _DEBUG 

   if(new_size < 0)
   {
	   std::cout << "Xiaoding Error: Vector Template." << std::endl 
		   << "Vector(int) constructor." << std::endl
                << "Size must be equal or greater than zero." <<endl;

      exit(1);
   }

   #endif
   
   size = new_size;
   display = true;
   data = new Type[new_size];
}

//创建一个大小为new_size的向量,并用类型为Type的值给向量赋初值
template <class Type>
Vector<Type> ::Vector(int new_size, const Type& value) 
{
   // Control sentence (if debug)      

   #ifdef _DEBUG 

   if(new_size < 0)
   {
	   std::cerr << "Xiaoding Error: Vector Template." << std::endl 
		   << "Vector(int, const Type&) constructor." << std::endl
		   << "Size must be equal or greater than zero." << std::endl;

      exit(1);
   }

   #endif
   
   size = new_size;
   display = true;
   data = new Type[new_size];
   
   for(int i = 0; i < size; i++)
   {
      data[i] = value;
   }
}

//复制构造函数,复制一个已经存在的向量,other_vector 为被赋值的向量
template <class Type>
Vector<Type>::Vector(const Vector<Type>& other_vector) 
{
   size = other_vector.size;
   display = true;
   data = new Type[size];

   // Control sentence (if debug)      
       
   for(int i = 0; i < size; i++)
   {
      data[i] = other_vector[i];
   }
}

//析构函数
template <class Type> 
Vector<Type>::~Vector(void)
{
   if(data != NULL)
   {
      delete[](data);
   }
}

//赋值运算符重载,将一个向量赋给另一个向量
template <class Type>
Vector<Type>& Vector<Type>::operator = (const Vector<Type>& other_vector)
{
   if(this != &other_vector) // other vector
   {
      if(size != other_vector.get_size()) // other size
      {
         if(data != NULL)
         {
            // Control sentence (if debug)      
       
            //#ifdef _DEBUG 
            //cout << "Xiaoding Warning: Vector Template." << endl 
            //          << "Vector& operator = (const Vector<Type>&)." << endl
            //          << "Assignment operator to non-empty vector." << endl;     
            //#endif

            delete [] (data);
         }

         size = other_vector.size;

         data = new Type[size];
      }

      for(int i = 0; i < size; i++)
      {
         data[i] = other_vector[i];
      }

	  display = true;//把other_vector的display的熟悉赋给它
   }

   return(*this);
}

//返回向量索引为i的值
template <class Type>
inline Type& Vector<Type>::operator [] (int i) 
{
   // Control sentence (if debug)

   #ifdef _DEBUG 

   if(size == 0)
   {
	   std::cerr << " Xiaoding Error: Vector Template. " << std::endl
                << "Reference operator []." << std::endl
                << "Size of vector is zero." << std::endl;

      exit(1);
   }
   else if(i < 0)
   {
      std::cerr << "Xiaoding Error: Vector Template. " << std::endl
                << "Reference operator []." << std::endl
                << "Index must be equal or greater than zero." << std::endl;

      exit(1);
   }
   else if(i >= size)
   {
      std::cerr << "Xiaoding Error: Vector Template. " << std::endl
                << "Reference operator []." << std::endl
                << "Index is " << i << " and it must be less than " << size << "." << std::endl;

      exit(1);
   }

   #endif

   // Return vector element

   return(data[i]);
}

template <class Type> 
std::ostream &operator<<(std::ostream &out, const Vector<Type>& V)  
{  
	
	for(int i=0;i<V.size;i++)  
    {  
		std::out<<V.data[i]<<" ";  
       if((i+1)%10==0)  
		   std::out<<endl;  
    }  
	return std::out;  
}  

//给向量加上一个值 vector+scalar
template <class Type>
Vector<Type> Vector<Type>::operator + (const Type& scalar) const
{       
   Vector<Type> sum(size);

   for(int i = 0; i < size; i++)
   {
      sum[i] = data[i] + scalar;
   }
   
   return(sum);
}

//一个向量加上另一个向量
template <class Type>
Vector<Type> Vector<Type>::operator + (const Vector<Type>& other_vector) const
{       
   // Control sentence (if debug)

   #ifdef _DEBUG 

   int other_size = other_vector.get_size();

   if(other_size != size)
   {
      std::cerr << "Xiaoding Error: Vector Template. " << std::endl
                << "Vector<Type> operator + (const Vector<Type>) const." << std::endl
                << "Size of vectors is " << size << " and " << other_size << " and they must be the same." 
                << std::endl;

      exit(1);          
   }

   #endif

   Vector<Type> sum(size);
  
   for(int i = 0; i < size; i++)
   {
      sum[i] = data[i] + other_vector[i];
   }
   
   return(sum);
}

template <class Type>
inline int Vector<Type>::get_size(void) const
{
   return(size);
}


}

#endif


 

 

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值