C++ベクトルVectorテンプレートの実装
プロジェクトでベクトルを使用する必要がある場合は、C++持参のvectorを使用できます.hはベクトルの記憶を実現し,ベクトルのクラスを自分で実現することもできる.
以下は自分で定義したベクトルクラスを与えて、どんな関数を必要としても自分で定義することができて、来るのがとても便利ではありませんか...
以下は自分で定義したベクトルクラスを与えて、どんな関数を必要としても自分で定義することができて、来るのがとても便利ではありませんか...
/*********************************************************************/
/* */
/* Xiaoding: Vector C++ Library */
/* */
/* */
/* Poject Speech */
/* Date:2012.03 */
/* E-mail: [email protected] */
/* */
/*********************************************************************/
#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