Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. template<class T>
  4. class vec{
  5. private:
  6. T* arr = nullptr;
  7. int size = 0;
  8. int capacity = 0;
  9. public:
  10. vec(int _size_ = 0):size(_size_),capacity(_size_+20){
  11. if(size<0 ){
  12. std::cout << "Incorrect value in Cons(1)" << std::endl;
  13.  
  14. }
  15. else
  16. arr = new T[capacity];
  17. }
  18. ~vec(){
  19. delete[] arr;
  20. }
  21. void push_back(T value){
  22. if(size == capacity){
  23. size++;
  24. capacity*=2;
  25. T* temp = new T[size];
  26. for(int i=0;i<size-1;i++){
  27. temp[i] = arr[i];
  28. }
  29. delete[] arr;
  30. arr = new T[capacity];
  31. for(int i=0;i<size-1;i++){
  32. arr[i] = temp[i];
  33. }
  34. arr[size-1] = value;
  35. delete[] temp;
  36. }else{
  37. size++;
  38. arr[size-1] = value;
  39. }
  40. }
  41. T& at(int n)const{
  42. return arr[n];
  43. }
  44. int getsize()const{
  45. return size;
  46. }
  47. int getcapacity()const{
  48. return capacity;
  49. }
  50. void resize(int n){
  51. if(n < capacity){
  52. capacity = n;
  53. size = n;
  54. T* temp = new T[capacity];
  55. for(int i=0;i<n;i++){
  56. temp[i] = arr[i];
  57.  
  58. }
  59. delete[] arr;
  60. arr = new T[capacity];
  61. for(int i=0;i<n;i++){
  62. arr[i] = temp[i];
  63. }
  64. delete[] temp;
  65. }else if(n>capacity){
  66. capacity = n;
  67. T* temp = new T[size];
  68. for(int i=0;i<size;i++){
  69. temp[i] = arr[i];
  70. }
  71. delete[] arr;
  72. arr = new T[capacity];
  73. for(int i=0;i<size;i++){
  74. arr[i] = temp[i];
  75. }
  76. delete[] temp;
  77. }
  78. }
  79. void print()const{
  80. std::cout << "-------------------" << std::endl;
  81. for(int i=0;i<size;i++){
  82. std::cout << arr[i] << " ";
  83. }
  84. std::cout << std::endl;
  85. }
  86. void printBack() const {
  87. if(size == 0){
  88. std::cout <<"No elements in this vector" << std::endl;
  89. }else
  90. std::cout << arr[size-1] << std::endl;
  91. }
  92. void printFront() const {
  93. if(size == 0){
  94. std::cout << "No elements in this vector" << std::endl;
  95. }else
  96. std::cout << arr[0] << std::endl;
  97. }
  98. void erase(T value){
  99. if(size == 0){
  100. std::cout << "Cant erase from null-value array" << std::endl;
  101. }else{
  102. for(int i=0;i<size;i++){
  103. if(arr[i] != value){
  104. continue;
  105. }
  106. else{
  107. for(int j=i;j<size-1;j++){
  108. arr[j] = arr[j+1];
  109. }
  110. size--;
  111. return;
  112. }
  113. }
  114. }
  115.  
  116. }
  117. void printcapacity()const{
  118. std::cout << capacity << std::endl;
  119. }
  120. void printsize()const{
  121. std::cout << size << std::endl;
  122. }
  123. void clear(){
  124. delete[] arr;
  125. capacity = 10;
  126. size = 0;
  127. arr = new T[capacity];
  128.  
  129. }
  130. T& operator[](int i)const{
  131. return arr[i];
  132. }
  133.  
  134. };
  135.  
  136. int main(){
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement