Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Vector.h"
- #include <iostream>
- template<class T>
- Vector<T>::Vector()
- {
- this -> size = 1;
- vec = new T[size];
- for (int i = 0; i < size; i++)
- {
- vec[i] = *new T;
- }
- }
- template <class T>
- Vector<T>::Vector(int size)
- {
- this->size = size;
- vec = new T[size];
- for (int i = 0; i < size; i++)
- {
- vec[i] = *new T;
- }
- /*T x = T;
- memset(vec, 0, size * sizeof(T));*/
- }
- template<class T>
- Vector<T>::Vector(Vector<T>& other)
- {
- /*if (vec != nullptr)
- delete[] vec;*/
- this->size = other.size;
- this->vec = new T[size];
- for (int i = 0; i < size; i++)
- {
- vec[i] = other.vec[i];
- }
- }
- template<class T>
- Vector<T>::~Vector()
- {
- //delete vec;
- }
- template<class T>
- void Vector<T>::push(int i, T newItem)
- {
- if (i >= 0 && i < size)
- vec[i] = newItem;
- else
- throw exception("index out of range");
- }
- template<class T>
- T Vector<T>::get(int index)
- {
- if (index >= 0 && index < size)
- return vec[index];
- else throw exception("index out of range");
- }
- template<class T>
- Vector<T> Vector<T>::operator+(Vector<T>& other)
- {
- Vector<T> newVec(size+other.size);
- int i = 0;
- for (i;i < size; i++)
- newVec.push(i, vec[i]);
- for (i; i < other.size + size; i++){
- newVec.push(i, other.vec[i - size]);
- }
- return newVec;
- }
- template<class T>
- void Vector<T>::operator=(Vector<T>& other)
- {
- size = other.size;
- vec = new T[size];
- for (int i = 0; i < size; i++)
- vec[i] = other.vec[i];
- }
- template <class T>
- ostream & operator<<(ostream & out, const Vector<T>& vec)
- {
- out << "vector[size: " << vec.size << ", values: [";
- for (int i = 0; i < vec.size; i++)
- {
- out << vec.vec[i];
- if (vec.size - i > 1)
- out << ", ";
- }
- out << "]" << endl;
- return out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement