Guest User

Untitled

a guest
Dec 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. template <class T>
  2. class vector{
  3. private:
  4. int length;
  5. int curSize;
  6. T *a;
  7. public:
  8. vector(){
  9. length = 1;
  10. curSize = 0;
  11. a = new T[length];
  12. }
  13.  
  14. vector(int x){
  15. length = x;
  16. curSize = 0;
  17. a = new T[length];
  18. }
  19.  
  20. void print(){
  21. for(int i=0; i<this->length; ++i){
  22. cout<<this->a[i]<<" ";
  23. }
  24. cout <<"\n";
  25. }
  26.  
  27. vector<T> operator=(const vector<T> &rhs){
  28. this->a = rhs.a;
  29. this->curSize = rhs.curSize;
  30. this->length = rhs.length;
  31. return *this;
  32. }
  33.  
  34. int len(){
  35. return length;
  36. }
  37.  
  38. int size(){
  39. return curSize;
  40. }
  41.  
  42. void push_back(int x){
  43. if(curSize < length){
  44. a[curSize] = x;
  45. curSize++;
  46. }else{
  47. length++;
  48. T *b = new T[length];
  49. for(int i=0; i<length; ++i){
  50. b[i] = a[i];
  51. }
  52. b[curSize] = x;
  53. curSize++;
  54. a = b;
  55. delete[] b;
  56. }
  57. }
  58.  
  59. T operator[](int index){
  60. if( index < curSize){
  61. return a[index];
  62. }
  63. }
  64.  
  65. vector<T> operator*=(T lmd){
  66. for(int i=0; i<this->size(); ++i){
  67. this->a[i]*=lmd;
  68. }
  69. return *this;
  70. }
  71.  
  72. vector<T> operator/=(T lmd){
  73. for(int i=0; i<curSize; ++i){
  74. this->a[i]/=lmd;
  75. }
  76. return *this;
  77. }
  78.  
  79. vector<T> operator+(const vector<T> &right){
  80. int n = this->curSize;
  81. int m = right.curSize;
  82. if(n == m){
  83. vector<T> b(n);
  84. for(int i=0; i<n; ++i){
  85. b.a[i] = this->a[i] + right.a[i];
  86. }
  87. return b;
  88. }
  89. }
  90.  
  91. vector<T> operator-(const vector<T> &right){
  92. int n = this->curSize;
  93. int m = right.curSize;
  94. if(n == m){
  95. vector<T> b(n);
  96. for(int i=0; i<n; ++i){
  97. b.a[i] = this->a[i] - right.a[i];
  98. }
  99. return b;
  100. }
  101. }
  102. };
Add Comment
Please, Sign In to add comment