⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vector.h

📁 矩阵计算的c++代码
💻 H
字号:
/******************************************
 * Copyright (c) 2004, LHD, IMech, CAS
 * All rights reserved.
 * 
 * Filename: Vector.h
 * File title:
 * Abstract: In this file, a __VectorType__-type vector is defined,
 *			together wiht some functions operating the vector.
 *			__VectorType__ is 'double' in this file.
 *			
 * 
 * Current Version:	1.0
 * Current Author:		Liu Changli (刘长礼)
 * Modification date:	Jan. 3, 2004
 *
 * 
 * Version 1.0, On Jan. 3, 2004, created by Liu Changli (刘长礼)
 ********************************************/

#pragma once
#ifndef __VECTOR_H
#define __VECTOR_H

#include "MyHeader.h"

using namespace std;
typedef double __VectorType__;

class CVector
{
private:
	typedef std::vector<__VectorType__>	DVector;
	typedef basic_filebuf<__VectorType__, char_traits<__VectorType__> > _Vec_filebuf;
	typedef basic_ifstream<__VectorType__, char_traits<__VectorType__> > _Vec_ifstream;
	typedef basic_ofstream<__VectorType__, char_traits<__VectorType__> > _Vec_ofstream;
	typedef basic_fstream<__VectorType__, char_traits<__VectorType__> > _Vec_fstream;
// constructor and destructor
public:
	CVector(void);
	virtual ~CVector(void);
	CVector(int nSize);
	CVector(const CVector& src);
	CVector(const vector<__VectorType__>& src);

// implemention
public:
	void resize(int nNewSize);	// create a new vector
	void Clear(void); // clear the vector	
	void Print(bool bRowCol=false)const;	// print to screen
	void Write(const string & sFilename, bool bApp=true, bool bBinAscii=true)const;	// write to a file
	void Read(const string & sFilename, bool bBinAscii=true)const;	// read from a file
	void Zeros(int nNewSize);	// create a zero vector
	void SetSize(int nNewSize);	// create a new vector
	int  size(void)const;	// get vector size
	int  GetLength(void)const;	// get the length of vector
	bool IsEmpty(void)const;	// test whether the vector contains no elements

//	operation of vector
public:
	CVector& operator =  (__VectorType__ src);	// set a __VectorType__ to the vector
	CVector& operator =  (const CVector& src);	// set a vector to the vector
	CVector& operator =  (const vector<__VectorType__>& src);	// set a vector to the vector

	//access
	const	__VectorType__& operator[](int nPos)const;	// get the value of vector at nPos
			__VectorType__& operator[](int nPos);	// get the value of vector at nPos
	const	__VectorType__& At(int nPos)const;	// get the value of vector at nPos
			__VectorType__& At(int nPos);	// get the value of vector at nPos

//	additional operations of vector
public:
	__VectorType__ Norm(void)const; // return the vector's 2 Norm
	//+++++++++++++++++++++++++++++++++++++++++++++++
	const CVector&  operator+()const;
	CVector& operator+=(__VectorType__ right);
	CVector& operator+=(const CVector& right);
	//-----------------------------------------------
	const CVector  operator-()const;
	CVector& operator-=(__VectorType__ right);
	CVector& operator-=(const CVector& right);
	//***********************************************
	CVector& operator*=(__VectorType__ right);
	/////////////////////////////////////////////////
	CVector& operator/=(__VectorType__ right);

// member data
private:
	int m_nSize;
	__VectorType__ * m_pData;
};

/////////////////////////////////////////////////////////////////////////////
//=========================================================================//
/////////////////////////////////////////////////////////////////////////////

CVector:: CVector(void) : m_nSize(0), m_pData(NULL)
{}
CVector:: ~CVector(void)
{	Clear();	}

CVector:: CVector(int nSize) : m_pData(NULL)
{	resize(nSize);	}
CVector:: CVector(const CVector& src) : m_pData(NULL)
{
	resize(src.m_nSize);
	memcpy(m_pData, src.m_pData, sizeof(__VectorType__) * m_nSize);
}
CVector:: CVector(const vector<__VectorType__>& src) : m_pData(NULL)
{
	resize(   (int) (src.size())   );
	for(int i=0; i<m_nSize; ++i)
	{	*(m_pData+i) = src[i];	}
}

// inline access
inline const __VectorType__& CVector::operator[](int nPos)const
{
	assert(nPos>=0 && nPos<m_nSize);
	return *(m_pData+nPos);
}
inline __VectorType__& CVector::operator[](int nPos)
{
	assert(nPos>=0 && nPos<m_nSize);
	return *(m_pData+nPos);
}
inline const __VectorType__& CVector::At(int nPos)const
{
	assert(nPos>=0 && nPos<m_nSize);
	return *(m_pData+nPos);
}
inline __VectorType__& CVector::At(int nPos)
{
	assert(nPos>=0 && nPos<m_nSize);
	return *(m_pData+nPos);
}
inline int  CVector::size(void)const	// get vector size
{	return m_nSize;	}
inline int  CVector::GetLength(void)const	// get the length of vector
{	size();	}
inline bool CVector::IsEmpty(void)const	// test whether the vector contains no elements
{	return (m_pData == NULL);	}

// non-inline access

// create the vector
void CVector::resize(int nNewSize)
{
	assert(nNewSize>=0);
	if(nNewSize == 0)	// clear the vector
	{	Clear();	}
	else if(IsEmpty())
	{
		m_pData = new __VectorType__ [nNewSize];
		m_nSize = nNewSize;
	}	
	else
	{
		__VectorType__* pData;
		pData = new __VectorType__  [nNewSize];

		int _M = min(m_nSize, nNewSize);
		memcpy(pData, m_pData, sizeof(__VectorType__) * _M);

		Clear();	// clear the vector
		m_pData = pData;
		m_nSize = nNewSize;
	}
}
void CVector::SetSize(int nNewSize)	// create a new vector
{	resize(nNewSize);	}

// destroy the vector
void CVector::Clear(void)
{
	delete [] m_pData; // free the header
	m_pData = NULL;
	m_nSize = 0;
}


// operations
CVector& CVector::operator=(__VectorType__ src)
{	// no m_pData is needed because this object has been constructed
	if(IsEmpty())
	{	return (*this);	}

	for(int i=0; i<m_nSize; ++i)
	{	*(m_pData+i) = src;	}
	return (*this);
}
CVector& CVector::operator=(const CVector& src)
{	// no m_pData is needed because this object has been constructed
	assert(src.m_nSize==m_nSize);
	if(IsEmpty())	return (*this);
	if(this == &src)	return (*this);	// 检查自赋值

	memcpy(m_pData, src.m_pData, sizeof(__VectorType__) * m_nSize);
	return (*this);
}

CVector& CVector::operator=(const vector<__VectorType__>& src)
{	// no m_pData is needed because this object has been constructed
	assert(src.size()==m_nSize);
	if(IsEmpty())	return (*this);

	for(int i=0; i<m_nSize; ++i)
	{	*(m_pData+i) = src[i];	}
	return (*this);
}
// create a zero vector
void CVector::Zeros(int nNewSize)
{
	resize(nNewSize);
	for(int i=0; i<m_nSize; ++i)
	{	*(m_pData+i) = 0.0;	}
}

//	print to screen
void CVector:: Print(bool bRowCol)const
{	// bRowCol==false 表示输出时候,是按照行的形式
	if(IsEmpty()) 
	{	cout<<"The vector is NULL"<<endl;	return;	}
	for(int i=0; i<m_nSize; ++i)
	{
		if(bRowCol)	cout<<*(m_pData+i)<<endl;
		else	cout<<*(m_pData+i)<<"  ";
	}
	cout<<endl;
}

// read from a file
void CVector::Read(const string & sFilename, bool bBinAscii)const
{	// bBinAscii==true means using ascii
	if(IsEmpty()) 
	{	cout<<"The vector is a NULL"<<endl;	return;	}

	if(bBinAscii)	// bBinAscii==true means that ascii is used
	{
		ifstream inFVector;
		inFVector.open(sFilename.c_str());
		assert(inFVector);
		// seek to begin of file
		inFVector.seekg(ios_base::beg);

		for(int i=0; i<m_nSize; ++i)
		{	inFVector>>*(m_pData+i);	}
	}
	else	// save matrix as a binary file
	{
		_Vec_ifstream inFVector;
		inFVector.open(sFilename.c_str(), ios_base::binary|ios_base::in);
		assert(inFVector);
		// seek to begin of file
		inFVector.seekg(ios_base::beg);

		for(int i=0; i<m_nSize; ++i)
		{	inFVector.read( (m_pData+i), 1 );	}
	}
}
// write to a file
void CVector::Write(const string & sFilename, bool bApp, bool bBinAscii)const
{	// bApp==true means appending a file; bBinAscii==true means using ascii
	if(IsEmpty()) 
	{	cout<<"The vector is NULL"<<endl;	return;	}

	if(bBinAscii)	// bBinAscii==true means that ascii is used
	{
		ofstream outFVector;
		if(bApp)
		{	// open a file and seek to end of file
			outFVector.open(sFilename.c_str(), ios_base::app);
			assert(outFVector);
			outFVector.seekp(ios_base::end);
		}
		else
		{	// open a file and seek to begin of file
			outFVector.open(sFilename.c_str(), ios_base::out);
			assert(outFVector);
			outFVector.seekp(ios_base::beg);	// 必须到文件头部位置,否则出错
		}

		for(int i=0; i<m_nSize; ++i)
		{	outFVector<<*(m_pData+i)<<"  ";	}
		outFVector<<endl;
	}
	else	// save matrix as a binary file
	{
		_Vec_ofstream outFVector;
		if(bApp)
		{	// open a file and seek to end of file
			outFVector.open(sFilename.c_str(), ios_base::binary|ios_base::app);
			assert(outFVector);
			outFVector.seekp(ios_base::end);
		}
		else
		{	// open a file and seek to begin of file
			outFVector.open(sFilename.c_str(), ios_base::binary|ios_base::out);
			assert(outFVector);
			outFVector.seekp(ios_base::beg);	// 必须到文件头部位置,否则出错
		}

		for(int i=0; i<m_nSize; ++i)
		{	outFVector.write( (m_pData+i), 1 );	}
	}
}

// return the vector's 2 Norm
__VectorType__ CVector::Norm(void)const
{
	if(IsEmpty())	// if the source matrix is null, return a negative value
	{	return (__VectorType__)(-1);	}

	__VectorType__ _result=0.0;
	for(int i=0; i<m_nSize; ++i)
	{	_result += (*(m_pData+i)) * (*(m_pData+i));	}
	return std::sqrt(_result);
}

//+++++++++++++++++++++++++++++++++++++++++++++++
const CVector&  CVector::operator+()const
{	return (*this);	}
CVector& CVector::operator+=(__VectorType__ right)
{
	if(IsEmpty())
	{	return (*this);	}

	for(int i=0; i<m_nSize; ++i)
	{	*(m_pData+i) += right;	}
	return (*this);
}
CVector& CVector::operator+=(const CVector& right)
{
	assert(m_nSize == right.size());
	if(IsEmpty())	return (*this);

	for(int i=0; i<m_nSize; ++i)
	{	*(m_pData+i) += *(right.m_pData+i);	}
	return (*this);
}

//-----------------------------------------------
const CVector  CVector::operator-()const
{
	if(IsEmpty())
	{	return (*this);	}

	CVector _result(m_nSize);
	for(int i=0; i<m_nSize; ++i)
	{	*(_result.m_pData+i) = - *(m_pData+i);	}
	
	return _result;
}
CVector& CVector::operator-=(__VectorType__ right)
{
	if(IsEmpty())
	{	return (*this);	}

	for(int i=0; i<m_nSize; ++i)
	{	*(m_pData+i) -= right;	}
	return (*this);
}
CVector& CVector::operator-=(const CVector& right)
{
	assert(m_nSize == right.size());
	if(IsEmpty())	return (*this);

	for(int i=0; i<m_nSize; ++i)
	{	*(m_pData+i) -= *(right.m_pData+i);	}
	return (*this);
}

//***********************************************
CVector& CVector::operator*=(__VectorType__ right)
{
	if(IsEmpty())
	{	return (*this);	}

	for(int i=0; i<m_nSize; ++i)
	{	*(m_pData+i) *= right;	}
	return (*this);
}

/////////////////////////////////////////////////
CVector& CVector::operator/=(__VectorType__ right)
{
	assert( right!=0.0 );
	if(IsEmpty())	return (*this);

	for(int i=0; i<m_nSize; ++i)
	{	*(m_pData+i) /= right;	}
	return (*this);
}

// define some additional friend operations of vector
#include "xxVector"

#endif // #ifndef __VECTOR_H

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -