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

📄 vector.h

📁 这是两个很好用的集合类
💻 H
📖 第 1 页 / 共 2 页
字号:

template <typename Type>
inline Vector<Type> Vector<Type>::operator + (Vector<Type> otherVector)
{       
   // Control sentence (if debug)

   #ifndef NDEBUG 

   int otherSize = otherVector.getSize();

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

      exit(1);          
   }

   #endif


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


//Vector<Type> operator - (Type) method 

/// Difference vector-scalar arithmetic operator.

template <typename Type>
inline Vector<Type> Vector<Type>::operator - (Type scalar)
{       
   Vector<Type> difference(size);

   for(int i = 0; i < size; i++)
   {
      difference[i] = vector[i] - scalar;
   }
   
   return(difference);
}


// Vector<Type> operator - (Vector<Type>)

/// Difference vector-vector arithmetic operator.

template <typename Type>
inline Vector<Type> Vector<Type>::operator - (Vector<Type> otherVector)
{       
   // Control sentence (if debug)      
       
   #ifndef NDEBUG 

   int otherSize = otherVector.getSize();

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

      exit(1);          
   }
      
   #endif

   Vector<Type> difference(size);
  
   for(int i = 0; i < size; i++)
   {
      difference[i] = vector[i] - otherVector[i];
   }
   
   return(difference);
}


// Vector<Type> operator * (Type) method 

/// Product vector*scalar arithmetic operator.

template <typename Type>
inline Vector<Type> Vector<Type>::operator * (Type scalar)
{       
   Vector<Type> product(size);

   for(int i = 0; i < size; i++)
   {
      product[i] = vector[i]*scalar;
   }
   
   return(product);
}


// Type operator * (Vector<Type>)  

/// Element by element product vector*vector arithmetic operator.

template <typename Type>
inline Vector<Type> Vector<Type>::operator * (Vector<Type> otherVector)
{       
   // Control sentence (if debug)      
       
   #ifndef NDEBUG 

   int otherSize = otherVector.getSize();

   if(otherSize != size)
   {
      std::cerr << std::endl
                << "Flood Error: Vector Template." << std::endl 
                << "Vector<Type> operator * (Vector<Type>)." << std::endl
                << "Both vector sizes must be the same." << std::endl
                << std::endl;

      exit(1);          
   }

   #endif

   Vector<Type> product(size);
  
   for(int i = 0; i < size; i++)
   {
      product[i] = vector[i]*otherVector[i];
   }
   
   return(product);
}


// Vector<Type> operator * (Matrix<Type>)  

/// Product vector*matrix arithmetic operator.

template <typename Type>
inline Vector<Type> Vector<Type>::operator * (Matrix<Type> matrix)
{
   int numberOfRows = matrix.getNumberOfRows();

   // Control sentence (if debug)      

   #ifndef NDEBUG 

   if(numberOfRows != size)
   {
      std::cerr << std::endl
                << "Flood Error: Vector Template." << std::endl 
                << "Type operator * (Matrix<Type>)." << std::endl
                << "Matrix number of rows must be equal to vector size." << std::endl
                << std::endl;

      exit(1);          
   }

   #endif

   int numberOfColumns = matrix.getNumberOfColumns();

   Vector<Type> product(numberOfColumns);
  
   for(int j = 0; j < numberOfColumns; j++)
   {     
      product[j] = 0;      

      for(int i = 0; i < numberOfRows; i++)
      {
         product[j] += vector[i]*matrix[i][j];
      }
   }
    
   return(product);
}
// Vector<Type> dot(Vector<Type>) method

/// Dot product vector*vector arithmetic operator.

template <typename Type>
inline Type Vector<Type>::dot(Vector<Type> otherVector)
{            
   // Control sentence (if debug)      

   #ifndef NDEBUG 

   int otherSize = otherVector.getSize();

   if(otherSize != size)
   {
      std::cerr << std::endl
                << "Flood Error: Vector Template." << std::endl 
                << "Type dot(Vector<Type>) method." << std::endl
                << "Both vector sizes must be the same." << std::endl
                << std::endl;

      exit(1);          
   }

   #endif

   Type dotProduct = 0;
  
   for(int i = 0; i < size; i++)
   {
      dotProduct += vector[i]*otherVector[i];
   }
   
   return(dotProduct);
}


// Matrix<Type> outer(Vector<Type>) method

/// Outer product vector*vector arithmetic operator.

template <typename Type>
inline Matrix<Type> Vector<Type>::outer(Vector<Type> otherVector)
{            
   int otherSize = otherVector.getSize();

   // Control sentence (if debug)      

   #ifndef NDEBUG 

   if(otherSize != size)
   {
      std::cerr << std::endl
                << "Flood Error: Vector Template." << std::endl 
                << "Matrix<Type> outer(Vector<Type>) method." << std::endl
                << "Both vector sizes must be the same." << std::endl
                << std::endl;

      exit(1);          
   }

   #endif

   int numberOfRows = size;
   int numberOfColumns = otherSize;
   
   Matrix<Type> outer(numberOfRows, numberOfColumns);
     
   for(int i = 0;  i < numberOfRows; i++)
   {
      for(int j = 0;  j < numberOfColumns; j++)
      {
         outer[i][j] = vector[i]*otherVector[j];
      }           
   }
   
   return(outer);
}


//Vector<Type> operator / (Type) method 

/// Cocient vector/scalar arithmetic operator.

template <typename Type>
inline Vector<Type> Vector<Type>::operator / (Type scalar)
{       
   Vector<Type> cocient(size);

   for(int i = 0; i < size; i++)
   {
      cocient[i] = vector[i]/scalar;
   }
   
   return(cocient);
}


// Vector<Type> operator - (Vector<Type>)

/// Cocient vector/vector arithmetic operator.

template <typename Type>
inline Vector<Type> Vector<Type>::operator / (Vector<Type> otherVector)
{       
   int otherSize = otherVector.getSize();

   // Control sentence (if debug)            

   #ifndef NDEBUG 

   if(otherSize != size)
   {
      std::cerr << std::endl
                << "Flood Error: Vector Template." << std::endl 
                << "Vector<Type> operator - (Vector<Type>)." << std::endl
                << "Both vector sizes must be the same." << std::endl
                << std::endl;

      exit(1);          
   }

   #endif

   Vector<Type> cocient(size);
  
   for(int i = 0; i < size; i++)
   {
      cocient[i] = vector[i]/otherVector[i];
   }
   
   return(cocient);
}


// Type* begin(void) method

/// This method returns a pointer to the first element in the container.

template <typename Type>
inline Type* Vector<Type>::begin()
{
   return(vector);
}


// Type* end(void) method

/// This method returns a pointer to the last element in the container. 

template <typename Type>
inline Type* Vector<Type>::end()
{
   return(vector + size);
}


// DESTRUCTOR

/// Destructor. 

template <typename Type> 
Vector<Type>::~Vector()
{
   if(vector != 0)
   {
      delete[](vector);
   }
}


// Input operator

/// This method re-writes the input operator >> for the Vector template. 

template<typename Type>
std::istream& operator>>(std::istream& is, Vector<Type>& v)
{
   int size = v.getSize();
   
   for(int i = 0; i < size; i++)
   {
      is >> v[i];
   }

   return(is);
}


// Output operator

/// This method re-writes the output operator << for the Vector template. 

template<typename Type>
std::ostream& operator<<(std::ostream& os, Vector<Type>& v)
{
   int size = v.getSize();
   
   for(int i = 0; i < size; i++)
   {
      os << v[i] << " ";
   }

   return(os);
}


// void load(char*) method

/// This method loads the elements of a vector from a data file.
/// The file format is as follows:
/// 
/// NumberOfElements
/// Element_0 Element_1 ... Element_N-1
///
/// @param filename Filename.
///
/// @see save(char*).

template <class Type>
inline void Vector<Type>::load(char* filename)
{
   std::fstream file;

   // Open file
    
   file.open(filename, std::ios::in);

   if(!file.is_open())
   {
      std::cerr << std::endl
                << "Flood Error: Vector template." << std::endl
                << "void load(char*) method." << std::endl
                << "Cannot open vector data file."  << std::endl
                << std::endl;

      exit(1);
   }
   else
   {
      std::cout << std::endl
                << "Loading vector from data file..." << std::endl;
   }

   // Read file

   file >> size;   

   resize(size);

   for(int i = 0; i < size; i++)
   {
      file >> vector[i];
   }

   // Close file

   file.close();
}


// void save(char*) method

/// This method saves to a data file the elements of the vector.
///
/// @param filename Filename.
///
/// @see load(char*).

template <class Type>
inline void Vector<Type>::save(char* filename)
{
   std::fstream file; 

   // Open file

   file.open(filename, std::ios::out);

   if(!file.is_open())
   {
      std::cerr << std::endl 
                << "Flood Error: Vector template." << std::endl
                << "void save(char*) method." << std::endl
                << "Cannot open vector data file." << std::endl
                << std::endl;

      exit(1);
   }
   else
   {
      std::cout << std::endl 
                << "Saving vector to data file..." 
                << std::endl;
   }

   // Write file

   file << size << std::endl;

   for(int i = 0; i < size; i++)
   {
      file << vector[i] << " ";
   }

   file << std::endl;

   // Close file

   file.close();
}

// void insert(int, Vector<Type>) method

/// Insert another vector starting from a given position.

template <typename Type>
inline void Vector<Type>::insert(int position, Vector<Type> otherVector)
{          int otherSize = otherVector.getSize();
   // Control sentence (if debug)            

   #ifndef NDEBUG 

   if(position + otherSize > size)
   {
      std::cerr << std::endl
                << "Flood Error: Vector Template." << std::endl 
                << "void insert(Vector<Type>, int) method." << std::endl
                << "Cannot insert vector." << std::endl
                << std::endl;

      exit(1);          
   }

   #endif

   for(int i = 0; i < otherSize; i++)
   {
      vector[position + i] = otherVector[i];
   }
}
// Vector<Type> extract(int, int) method

/// Extract a vector of a given size from a given position

template <typename Type>
inline Vector<Type> Vector<Type>::extract(int position, int otherSize)
{               // Control sentence (if debug)            

   #ifndef NDEBUG 

   if(position + otherSize > size)
   {
      std::cerr << std::endl
                << "Flood Error: Vector Template." << std::endl 
                << "Vector<Type> extract(int, int) method." << std::endl
                << "Cannot extract vector." << std::endl
                << std::endl;

      exit(1);          
   }

   #endif
   Vector<Type> otherVector(otherSize);
   for(int i = 0; i < otherSize; i++)
   {
      otherVector[i] = vector[position + i];
   }   return(otherVector);
}

// Vector<Type> assemble(Vector<Type>) method

/// Assemble two vectors.

template <typename Type>
inline Vector<Type> Vector<Type>::assemble(Vector<Type> otherVector)
{            
   int otherSize = otherVector.getSize();

   Vector<double> assembly(size + otherSize);

   for(int i = 0; i < size; i++)
   {
      assembly[i] = vector[i];
   }

   for(int i = 0; i < otherSize; i++)
   {
      assembly[size+i] = otherVector[i];
   } 
   
   return(assembly);
}


}

#endif


⌨️ 快捷键说明

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