📄 vector.hh
字号:
Vector<int> v(n); int i; for(i=0; i<n; i++) v[i] = (int)a[i]; return v;} template<class Type>Vector<long> Vector<Type>::operator Vector<long>() const{ Vector<long> v(n); int i; for(i=0; i<n; i++) v[i] = (long)a[i]; return v;}*/ //overloading = operatortemplate<class Type>Vector<Type>& Vector<Type>::operator=(const Vector<Type>& v){ if (n != v.n) { n = v.n; delete [] a; a = new Type[n]; } for (int i=0; i<n; i++) a[i] = (Type) v[i]; return *this;}/*template<class Type>Vector<Type>& Vector<Type>::operator=(int m, const Vector<Type>& v){ for (int i=0; i<min(m,n); i++) a[i] = (Type) v[i]; return *this;}*/template<class Type>Vector<Type>& Vector<Type>::operator=(Type c){ for (int i=0; i<n; i++) a[i] = c; return *this;} template<class Type>Vector<Type>& Vector<Type>::operator=(Type *p){ for (int i=0; i<n; i++) a[i] = p[i]; return *this;} //overlodaing += operatortemplate<class Type>Vector<Type>& Vector<Type>::operator+=(const Vector<Type>& v){ if (n!=v.n) inValidSize(); for (int i=0; i<Min(v.n, n); i++) a[i] += v.a[i]; return *this;} template<class Type>Vector<Type>& Vector<Type>::operator+=(Type c){ for (int i=0; i<n; i++) a[i] += c; return *this;} template<class Type>Vector<Type>& Vector<Type>::operator+=(Type *p){ for (int i=0; i<n; i++) a[i] += p[i]; return *this;} //overloading -= operatortemplate<class Type>Vector<Type>& Vector<Type>::operator-=(const Vector<Type>& v){ if (n!=v.n) inValidSize(); for (int i=0; i<Min(v.n, n); i++) a[i] -= v.a[i]; return *this;} template<class Type>Vector<Type>& Vector<Type>::operator-=(Type c){ for (int i=0; i<n; i++) a[i] -= c; return *this;} template<class Type>Vector<Type>& Vector<Type>::operator-=(Type *p){ for (int i=0; i<n; i++) a[i] -= p[i]; return *this;} //overloading *= /= operatorstemplate<class Type>Vector<Type>& Vector<Type>::operator*=(Type c){ for (int i=0; i<n; i++) a[i] *= c; return *this;} template<class Type>Vector<Type>& Vector<Type>::operator/=(Type c){ assert(c != 0); for(int i=0; i<n; i++) a[i] /= c; return *this;} //overloading insertion and extraction operatorstemplate<class Type>ostream& operator<<(ostream& ofp, const Vector<Type>& v){ ofp<<v.a[0]<<" "; for(int i=1; i<v.n; i++) { if (i%5 == 0) ofp << "\n"; ofp<<v.a[i]<<" "; } ofp<<endl; return ofp;} template<class Type>istream& operator>>(istream& ifp, Vector<Type>& v){ for(int i=0; i<v.n; i++) if (!(ifp>>v.a[i])) break; return ifp;} /*template<class Type>size_t Vector<Type>::bfread(FILE *ifp){ return fread(a, sizeof(Type), n, ifp);}*/template<class Type>size_t Vector<Type>::bfwrite(FILE *ofp) { return fwrite(a, sizeof(Type), n, ofp);} template<class Type>Vector<Type>& Vector<Type>::chaSize(int m) { Vector<Type> v(n, a); delete [] a; a = new Type[m]; for(int i=0; i<m; i++) { if (i<n) a[i] = v[i]; else a[i] = 0; } n = m; return *this;}//adding elements to the vectortemplate<class Type>Vector<Type>& Vector<Type>::addVal( Type c){ chaSize(n+1); a[n-1] = c; return *this;} template<class Type>Vector<Type>& Vector<Type>::addVal(const Vector<Type>& u){ int m = u.n, oldn = n; chaSize(oldn+m); for (int i=0; i < m; i++) a[i+oldn] = u.a[i]; return *this;} template<class Type>Vector<Type>& Vector<Type>::addVal(Type c, int index){ Vector<Type> u(n-index); for (int i=0; i < n-index; i++) u[i] = a[i+index]; chaSize(index); addVal(c); cerr << "length: "<<n <<endl; addVal(u); cerr << "length: "<<n <<endl; return *this;}template<class Type>Vector<Type>& Vector<Type>::delVal(int ielem){ int i; Vector<Type> temp(*this); delete [] a; n --; a = new Type[n]; for (i=0; i<ielem; i++) a[i] = temp[i]; for (i=ielem; i<n; i++) a[i] = temp[i+1]; return *this;} //inline functionstemplate<class Type>inline Type operator*(const Vector<Type>& u, const Vector<Type>& v){ int n = u.size(); if (n != v.size()) inValidSize(); Type sum = 0, temp1, temp2; for(int i=0; i<n; i++) { temp1 = u[i]; temp2 = v[i]; sum += temp1*temp2; } return sum;} template<class Type>inline Vector<Type> operator*( Type c, const Vector<Type>& v){ Vector<Type> u(v); u *= c; return u;} template<class Type>inline Vector<Type> operator*(const Vector<Type>& v, Type c){ Vector<Type> u(v); u *= c; return u;} template<class Type>inline Vector<Type> operator/(const Vector<Type>& v, Type c){ assert(c != 0); Vector<Type> u(v); u /= c; return u;} template<class Type>inline Vector<Type> operator+(const Vector<Type>& v, Type c){ Vector<Type> w(v); w += c; return w; } template<class Type>inline Vector<Type> operator+( Type c, const Vector<Type>& v){ Vector<Type> w(v); w += c; return w; } template<class Type>inline Vector<Type> operator+(const Vector<Type>& v, const Vector<Type>& u){ Vector<Type> w(v); w += u; return w; } template<class Type>inline Vector<Type> operator-(const Vector<Type>& v, Type c){ Vector<Type> w(v); w -= c; return w; } template<class Type>inline Vector<Type> operator-( Type c, const Vector<Type>& v){ Vector<Type> w(v); w -= c; return -w; } template<class Type>inline Vector<Type> operator-(const Vector<Type>& v, const Vector<Type>& u){ Vector<Type> w(v); w -= u; return w; } template<class Type>inline Vector<Type> operator-(const Vector<Type>& v, Type* p){ Vector<Type> w(v); w -= p; return w; } template<class Type>Type Vector<Type>::norm2S() const { Vector<Type> v(*this); return (v*v);} template<class Type> //circular index of elements, 01/27/95Type Vector<Type>::CircElem(int i) const{ int j; if (i<0) j = i + (Abs(i)/n +1)*n; else j = i%n; return a[j];} template<class Type>inline Vector<Type> saxpy( Type factor, const Vector<Type>& x, const Vector<Type>& y){ Vector<Type> v(factor*x); v += y; return v;} template<class Type> // 08/04/94, H.L. DengVector<Type>* Vector<Type>::copy(int ibeg, int iend){ int ilength = iend-ibeg+1; if (ilength <= 0) { cerr << "Index is not correct!"<<endl; return NULL; } Vector<Type>* u = new Vector<Type>(ilength); for (int i=0; i<ilength; i++) u[0][i] = a[i+ibeg]; return u;} template<class Type>Vector<Type>* Vector<Type>::copy(int ilength){ if (ilength <= 0) { cerr << "The length is not correct!"<<endl; return NULL; } Vector<Type>* u = new Vector<Type>(ilength); for (int i=0; i<ilength; i++) u[0][i] = a[i]; return u;}template<class Type>int operator!=(const Vector<Type>& u, const Vector<Type>& v){ int i, n=u.size(); if (n!=v.size()) return 1; for (int i=0, i<n; i++) if (u[i] != v[i]) return 1; return 0;}template<class Type> int operator==(const Vector<Type>& u, const Vector<Type>& v){ int i, n=u.size(); if (n!=v.size()) return 0; for (int i=0, i<n; i++) if (u[i] != v[i]) return 0; return 1;}template<class Type>int operator!=(const Vector<Type>& u, Type c){ for (int i=0; i<u.size(); i++) if (u != c) return 1; return 0;}template<class Type>int operator==(const Vector<Type>& u, Type c){ for (int i=0; i<u.size(); i++) if (u != c) return 0; return 1;}template<class Type> // H.L. Deng, 07/25/95int Vector<Type>::in(Type c) const { for (int i=0; i<n; i++) { if (a[i] == c) return i; continue; } return -1;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -