Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // Assume Vector only takes in int or double for T
  6.  
  7. template<typename T> class Vector {
  8.  
  9. private:
  10.     int sz;     // the number of elements in this Vector
  11.     T* buf;     // the base of the array of Ts, you must allocate it
  12.  
  13. public:
  14.  
  15.     Vector();
  16.  
  17.     Vector(int n); // Vector v1(10); -- create a 10 element Vector
  18.  
  19.     Vector(initializer_list<T> L); // Vector v1{T1, T2, T3};
  20.  
  21.     //~Vector();
  22.     /* destructor called automatically when a Vector dies.
  23.     Destructor should free memory used. your program should have no leak/lost/still-reachable/errors(suppressed or not),
  24.      besides 72704 bytes in one still-reachable block (a g++/valgrind bug on some versions). */
  25.  
  26.     //Vector(const Vector & v); // Vector v2(v1); deep-copy
  27.  
  28.     int size() const; // v1.size() returns 10 for v1 example above
  29.    
  30.     T & operator [] (const int i); /* T x = V[i];
  31. Access out-of-bound index should throw an error to be caught in outside scope */
  32.     T operator * (const Vector & v) const;
  33. // T x = V1 * V2; dot product
  34. // e.g. [1, 2] * [3, 4, 5] = 1 * 3 + 2 * 4 + 0 = 11
  35. // Assume an empty Vector will cause the product to be 0.
  36.     Vector operator + (const Vector & v) const;
  37. // V3 = V1 + V2; [1, 2, 3] + [4, 5, 6, 7] = [5, 7, 9, 7]
  38.     const Vector & operator = (const Vector & v); // V1 = V2;
  39.     bool operator == (const Vector & v) const; // if (V1 == V2)...
  40.     bool operator != (const Vector & v) const; // if (V1 != V2)...
  41.     friend Vector operator * (const int n, const Vector & v);
  42.     // V1 = 20 * V2; -- each element of V1 is element of V2 * 20
  43.     friend Vector operator + (const int n, const Vector & v);
  44.     // V1 = 20 + V2; -- each element of V1 is element of V2 + 20
  45.     friend ostream& operator << (ostream & o, const Vector & v);
  46.     // cout << V2; -- prints the vector in this format
  47. // (v0, v1, v2, ... vn-1);
  48.  
  49.    
  50. };
  51.  
  52. template<typename T>
  53. Vector<T>::Vector(){
  54.     sz = 0;
  55.     buf = new T[0];
  56. }
  57.  
  58. template<typename T>
  59. Vector<T>::Vector(int n){
  60.     sz = n;
  61.     buf = new T[n];
  62. }
  63.  
  64. template<typename T>
  65. Vector<T>::Vector(initializer_list<T> L){
  66.     sz = L.size();
  67.     buf = new T[sz];
  68.     int index = 0;
  69.     for (auto it = L.begin(); it != L.end(); it++){
  70.         buf[index] = *it;
  71.     }
  72. }
  73.  
  74. template<typename T>
  75. int Vector<T>::size() const{
  76.     return sz;
  77. }
  78.  
  79. int main(){
  80.     Vector<int> vect{3,4,5};
  81.     cout << vect.size();
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement