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

📄 vector.cpp

📁 斯坦福Energy211/CME211课《c++编程——地球科学科学家和工程师》的课件
💻 CPP
字号:
// Implementation file for Vector class#include <iostream>#include <cmath>#include "vector.h"using namespace std;// Public membersVector Vector::operator,( double x ) const{	// Create new vector containing this vector's data, with	// x added to the end	Vector v( m_length + 1 );	// Copy this vector's data	v.Set( m_data, m_length );	// Now add x to the end	v[m_length] = x;	return v;}Vector Vector::operator,( const Vector& v ) const{	// Concatenate this vector and v to create new vector w	Vector w( m_length + v.m_length );	// Copy this vector's data	w.Set( m_data, m_length );	// Copy v's data to the end of w	w.Set( v.m_data, v.m_length, m_length );	return w;}double& Vector::operator()( int i ) const{	// Make sure index is valid	if ( i < 0 || i >= m_length )		throw exception();	// Return a reference so that this can be used on the	// left-hand side of an assignment operator	return m_data[i];}double Vector::operator()( double x ) const{	// x is treated as an "index", make sure it's valid	if ( x < 0.0 || x >= m_length - 1.0 )		throw exception();	// Linearly interpolate between elements at nearest	// integer indices	double x1 = floor( x );	double x2 = ceil( x );	double theta = x - x1;	return ( 1.0 - theta ) * m_data[(int) x1] + theta * m_data[(int) x2];}Vector Vector::operator()( int *indices, int length ) const{	// Create new vector containing elements at given	// indices	Vector v( length );	for ( int i = 0; i < length; i++ )	{		// Make sure each index is valid		if ( indices[i] < 0 || indices[i] >= m_length )			throw exception();		v[i] = m_data[indices[i]];	}	return v;}Vector& Vector::operator=( const Vector& v ){	// Set new length, and store v's data in this vector	m_length = v.m_length;	allocate();	Set( v.m_data );	return *this;}Vector& Vector::operator=( double x ){	// Create a 1-element vector containing x	m_length = 1;	allocate();	Set( &x );		return *this;}double& Vector::operator[]( int i ) const{	// Make sure index is valid	if ( i < 0 || i >= m_length )		throw exception();	// Return a reference so that this can be used on the	// left-hand side of an assignment operator	// Can also use m_data[i]	return *( m_data + i );}// Private membersvoid Vector::allocate(){	// Always destroy old data before allocating new	deallocate();	if ( m_length > 0 )		m_data = new double[m_length];}void Vector::deallocate(){	delete [] m_data;	m_data = NULL;}void Vector::Set( double *data, int length, int offset ){	// Default value of length is -1	// In this case, we assume that data has a number	// of elemens equal to the length of the vector	if ( length == -1 )		length = m_length;	// Make sure indices will be valid	if ( offset < 0 || length + offset > m_length )		throw exception();	// Store values at designated locations	for ( int i = 0; i < length; i++ )		m_data[i+offset] = data[i];}// Insertion (output) operatorostream& operator<<( ostream& out, const Vector& v ){	// Print elements in a column, with field width of 10	for ( int i = 0; i < v.get_length(); i++ )	{		out.width( 10 );		out << v[i] << endl;	}	return out;}

⌨️ 快捷键说明

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