📄 vector.h
字号:
cerr << "Error! It's impossible to sum two vectors with different length." << endl;
}
}
template<class T>
Vector<T>* Vector<T>::SubtractVector (Vector<T>* V1, Vector<T>* V2) {
if (V1->GetLength() == V2->GetLength()) {
Vector *V3 = V1->Clone();
for (int i=0; i<V1->GetLength(); i++) {
V3->Values[i] -= V2->Values[i];
}
return V3;
}
else {
cerr << "Error! It's impossible to subtract two vectors with different length." << endl;
return new Vector<T>();
}
}
template<class T>
void Vector<T>::ProductVector (Vector* V)
{
if (this->GetLength() == V->GetLength()) {
for (int i=0; i<this->GetLength(); i++) {
this->Values[i] *= V->Values[i];
}
}
else {
cerr << "Error! It's impossible to sum two vectors with different length." << endl;
}
}
template<class T>
Vector<T>* Vector<T>::ProductVector (Vector<T>* V1, Vector<T>* V2)
{
if (V1->GetLength() == V2->GetLength()) {
Vector *V3 = new Vector(V1->Values, V1->GetLength());
for (int i=0; i<V1->GetLength(); i++) {
V3->Values[i] *= V2->Values[i];
}
return V3;
}
else {
cerr << "Error! It's impossible to sum two vectors with different length." << endl;
return new Vector<T>();
}
}
template<class T>
T Vector<T>::ProductVectorScalar (Vector<T>* V)
{
if (this->GetLength() == V->GetLength()) {
T Product = 0;
for (int i=0; i<this->GetLength(); i++) {
Product += this->Values[i] * V->Values[i];
}
return Product;
}
else {
cerr << "Error! It's impossible to sum two vectors with different length." << endl;
T Product;
return Product;
}
}
template<class T>
T Vector<T>::ProductVectorScalar (Vector<T>* V1, Vector<T>* V2)
{
if (V1->GetLength() == V2->GetLength()) {
T Product = 0;
for (int i=0; i<V1->GetLength(); i++) {
Product += V1->Values[i] * V2->Values[i];
}
return Product;
}
else {
cerr << "Error! It's impossible to multiply two vectors with different length." << endl;
T Product;
return Product;
}
}
template<class T>
T Vector<T>::Sum()
{
T X = 0;
for (int i=0; i<this->Length; i++) {
X += this->Values[i];
}
return X;
}
template<class T>
T Vector<T>::AbsSum()
{
T X = 0;
for (int i=0; i<this->Length; i++) {
X += ABS(this->Values[i]);
}
return X;
}
// Comparison Operations
template<class T>
T Vector<T>::Min()
{
if (this->Length>0) {
T MinValue = this->Values[0];
for (int i=1; i<this->Length; i++) {
if (this->Values[i]<MinValue) {
MinValue = this->Values[i];
}
}
return MinValue;
}
else {
T MinValue;
return MinValue;
}
}
template<class T>
void Vector<T>::Min(T* MinValue, int* MinIndex)
{
if (this->Length>0) {
(*MinValue) = this->Values[0];
(*MinIndex) = 0;
for (int i=1; i<this->Length; i++) {
if (this->Values[i]<(*MinValue)) {
(*MinValue) = this->Values[i];
(*MinIndex) = i;
}
}
}
else {
(*MinValue) = -1;
(*MinIndex) = -1;
}
}
template<class T>
T Vector<T>::MinAbs()
{
if (this->Length>0) {
T MinValue = ABS(this->Values[0]);
for (int i=1; i<this->Length; i++) {
if (ABS(this->Values[i])<MinValue) {
MinValue = ABS(this->Values[i]);
}
}
return MinValue;
}
else {
T MinValue;
return MinValue;
}
}
template<class T>
void Vector<T>::MinAbs(T* MinValue, int* MinIndex)
{
if (this->Length>0) {
(*MinValue) = ABS(this->Values[0]);
(*MinIndex) = 0;
for (int i=1; i<this->Length; i++) {
if (ABS(this->Values[i])<(*MinValue)) {
(*MinValue) = ABS(this->Values[i]);
(*MinIndex) = i;
}
}
}
else {
(*MinValue) = -1;
(*MinIndex) = -1;
}
}
template<class T>
T Vector<T>::Max()
{
if (this->Length>0) {
T MaxValue = this->Values[0];
for (int i=1; i<this->Length; i++) {
if (this->Values[i]>MaxValue) {
MaxValue = this->Values[i];
}
}
return MaxValue;
}
else {
T MaxValue;
return MaxValue;
}
}
template<class T>
void Vector<T>::Max(T* MaxValue, int* MaxIndex)
{
if (this->Length>0) {
(*MaxValue) = this->Values[0];
(*MaxIndex) = 0;
for (int i=1; i<this->Length; i++) {
if (this->Values[i]>(*MaxValue)) {
(*MaxValue) = this->Values[i];
(*MaxIndex) = i;
}
}
}
else {
(*MaxValue) = -1;
(*MaxIndex) = -1;
}
}
template<class T>
T Vector<T>::MaxAbs()
{
if (this->Length>0) {
T MaxValue = ABS(this->Values[0]);
for (int i=1; i<this->Length; i++) {
if (ABS(this->Values[i])>MaxValue) {
MaxValue = ABS(this->Values[i]);
}
}
return MaxValue;
}
else {
T MaxValue;
return MaxValue;
}
}
template<class T>
void Vector<T>::MaxAbs(T* MaxValue, int* MaxIndex)
{
if (this->Length>0) {
(*MaxValue) = ABS(this->Values[0]);
(*MaxIndex) = 0;
for (int i=1; i<this->Length; i++) {
if (ABS(this->Values[i])>(*MaxValue)) {
(*MaxValue) = ABS(this->Values[i]);
(*MaxIndex) = i;
}
}
}
else {
(*MaxValue) = -1;
(*MaxIndex) = -1;
}
}
template<class T>
T Vector<T>::Mean()
{
if (this->Length>0) {
T MeanValue = 0;
for (int i=0; i<this->Length; i++) {
MeanValue += this->Values[i];
}
return MeanValue/this->Length;
}
else {
T MeanValue;
return MeanValue;
}
}
template<class T>
T Vector<T>::MeanAbs()
{
if (this->Length>0) {
T MeanValue = 0;
for (int i=0; i<this->Length; i++) {
MeanValue += ABS(this->Values[i]);
}
return MeanValue/this->Length;
}
else {
T MeanValue;
return MeanValue;
}
}
template<class T>
T Vector<T>::Variance()
{
if (this->Length>0) {
T Variance = 0;
T MeanError = this->MeanAbs();
for (int i=1; i<this->Length; i++) {
Variance += (ABS(this->Values[i]) - MeanError)*(ABS(this->Values[i]) - MeanError);
}
if (this->Length>1)
return Variance/(this->Length-1);
else
return Variance;
}
else {
T Variance;
return Variance;
}
}
// Sorting Operations
template<class T>
void Vector<T>::Sort()
{
sort(&this->Values[0], &this->Values[this->Length-1]);
}
template<class T>
void Vector<T>::RemoveDuplicates()
{
for (int i=this->Length-1; i>0; i--) {
for (int j=0; j<i; j++) {
if (this->Values[i]==this->Values[j]) {
this->RemoveAt(i);
break;
}
}
}
}
template<class T>
int Vector<T>::Find(T X)
{
for (int i=0; i<this->Length; i++) {
if (this->Values[i] == X) {
return i;
}
}
return -1;
}
// I/O Operations
template<class T>
Vector<T>* Vector<T>::Load(char* Filename)
{
// Open the file
ifstream File (Filename, ios::in);
if (!File) {
cerr << "Error. It's impossible to open the file." << endl;
return new Vector<T>();
}
Vector<T>* V;
// Load the vector
try {
T Value;
V = new Vector<T>();
while (!File.eof()) {
File >> Value;
V->Add(Value);
}
}
catch (...) {
//cerr << "Error. It's impossible to complete the save." << endl;
}
// Close the file
File.close();
return V;
}
template<class T>
void Vector<T>::Save (char* Filename)
{
// Open the file
ofstream File (Filename, ios::out);
if (!File) {
cerr << "Error. It's impossible to create the file." << endl;
return;
}
File.precision(30);
// Save the vector
try {
for (int i=0; i<this->Length; i++)
File << this->Values[i] << " ";
File << endl;
}
catch (...) {
cerr << "Error. It's impossible to complete the save." << endl;
}
// Close the file
File.close();
}
template<class T>
void Vector<T>::Print ()
{
for (int i=0; i<this->Length; i++) {
cout << this->Values[i] << "\t";
}
cout << endl;
}
template<class T>
void Vector<T>::Print(char *VectorName)
{
cout << VectorName << " \t";
Print();
}
// Operators Redefinition
template<class T>
T Vector<T>::operator [] (int Index)
{
return this->Values[Index];
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -