Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // Assume Vector only takes in int or double for T
- template<typename T> class Vector {
- private:
- int sz; // the number of elements in this Vector
- T* buf; // the base of the array of Ts, you must allocate it
- public:
- Vector();
- Vector(int n); // Vector v1(10); -- create a 10 element Vector
- Vector(initializer_list<T> L); // Vector v1{T1, T2, T3};
- //~Vector();
- /* destructor called automatically when a Vector dies.
- Destructor should free memory used. your program should have no leak/lost/still-reachable/errors(suppressed or not),
- besides 72704 bytes in one still-reachable block (a g++/valgrind bug on some versions). */
- //Vector(const Vector & v); // Vector v2(v1); deep-copy
- int size() const; // v1.size() returns 10 for v1 example above
- T & operator [] (const int i); /* T x = V[i];
- Access out-of-bound index should throw an error to be caught in outside scope */
- T operator * (const Vector & v) const;
- // T x = V1 * V2; dot product
- // e.g. [1, 2] * [3, 4, 5] = 1 * 3 + 2 * 4 + 0 = 11
- // Assume an empty Vector will cause the product to be 0.
- Vector operator + (const Vector & v) const;
- // V3 = V1 + V2; [1, 2, 3] + [4, 5, 6, 7] = [5, 7, 9, 7]
- const Vector & operator = (const Vector & v); // V1 = V2;
- bool operator == (const Vector & v) const; // if (V1 == V2)...
- bool operator != (const Vector & v) const; // if (V1 != V2)...
- friend Vector operator * (const int n, const Vector & v);
- // V1 = 20 * V2; -- each element of V1 is element of V2 * 20
- friend Vector operator + (const int n, const Vector & v);
- // V1 = 20 + V2; -- each element of V1 is element of V2 + 20
- friend ostream& operator << (ostream & o, const Vector & v);
- // cout << V2; -- prints the vector in this format
- // (v0, v1, v2, ... vn-1);
- };
- template<typename T>
- Vector<T>::Vector(){
- sz = 0;
- buf = new T[0];
- }
- template<typename T>
- Vector<T>::Vector(int n){
- sz = n;
- buf = new T[n];
- }
- template<typename T>
- Vector<T>::Vector(initializer_list<T> L){
- sz = L.size();
- buf = new T[sz];
- int index = 0;
- for (auto it = L.begin(); it != L.end(); it++){
- buf[index] = *it;
- }
- }
- template<typename T>
- int Vector<T>::size() const{
- return sz;
- }
- int main(){
- Vector<int> vect{3,4,5};
- cout << vect.size();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement