Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <class T>
- class vector{
- private:
- int length;
- int curSize;
- T *a;
- public:
- vector(){
- length = 1;
- curSize = 0;
- a = new T[length];
- }
- vector(int x){
- length = x;
- curSize = 0;
- a = new T[length];
- }
- void print(){
- for(int i=0; i<this->length; ++i){
- cout<<this->a[i]<<" ";
- }
- cout <<"\n";
- }
- vector<T> operator=(const vector<T> &rhs){
- this->a = rhs.a;
- this->curSize = rhs.curSize;
- this->length = rhs.length;
- return *this;
- }
- int len(){
- return length;
- }
- int size(){
- return curSize;
- }
- void push_back(int x){
- if(curSize < length){
- a[curSize] = x;
- curSize++;
- }else{
- length++;
- T *b = new T[length];
- for(int i=0; i<length; ++i){
- b[i] = a[i];
- }
- b[curSize] = x;
- curSize++;
- a = b;
- delete[] b;
- }
- }
- T operator[](int index){
- if( index < curSize){
- return a[index];
- }
- }
- vector<T> operator*=(T lmd){
- for(int i=0; i<this->size(); ++i){
- this->a[i]*=lmd;
- }
- return *this;
- }
- vector<T> operator/=(T lmd){
- for(int i=0; i<curSize; ++i){
- this->a[i]/=lmd;
- }
- return *this;
- }
- vector<T> operator+(const vector<T> &right){
- int n = this->curSize;
- int m = right.curSize;
- if(n == m){
- vector<T> b(n);
- for(int i=0; i<n; ++i){
- b.a[i] = this->a[i] + right.a[i];
- }
- return b;
- }
- }
- vector<T> operator-(const vector<T> &right){
- int n = this->curSize;
- int m = right.curSize;
- if(n == m){
- vector<T> b(n);
- for(int i=0; i<n; ++i){
- b.a[i] = this->a[i] - right.a[i];
- }
- return b;
- }
- }
- };
Add Comment
Please, Sign In to add comment