Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1.  
  2. //change the capacity
  3. void Vector::reserve(int n)
  4. {
  5. int num;
  6. if (this->_capacity < n)
  7. {
  8. num = ((n - this->_capacity) / this->_resizeFactor) + 1;
  9. this->make_more_space(num);
  10. }
  11. }
  12.  
  13.  
  14. //change the size
  15. void Vector::resize(int n)
  16. {
  17. int i;
  18. if (this->_capacity >= n)
  19. {
  20. for (i = this->_capacity; i >= n; i--)
  21. {
  22. this->_elements[i] = 0;
  23. this->_size -= 1;
  24. }
  25. }
  26. else
  27. {
  28. this->reserve(n);
  29. }
  30. }
  31.  
  32. //assigns val to all elemnts
  33. void Vector::assign(int val)
  34. {
  35. int i;
  36. for (i = 0; i < this->_size -1; i++)
  37. {
  38. this->_elements[i] = val;
  39. }
  40. }
  41.  
  42. //change the size
  43. void Vector::resize(int n, const int& val)
  44. {
  45. int i;
  46. if (this->_capacity >= n)
  47. {
  48. for (i = this->_capacity; i >= n; i--)
  49. {
  50. this->_elements[i] = 0;
  51. this->_size -= 1;
  52. }
  53. }
  54. else
  55. {
  56. this->reserve(n);
  57. }
  58. this->assign(val);
  59. }
  60.  
  61. Vector::Vector(const Vector& other)
  62. {
  63. this->_elements = new int(*(other._elements));
  64. this->_size = other._size;
  65. this->_capacity = other._capacity;
  66. this->_resizeFactor = other._resizeFactor;
  67. }
  68.  
  69. Vector& Vector::operator=(const Vector& other)
  70. {
  71. delete[]this->_elements;
  72. this->_elements = new int(*(other._elements));
  73. this->_size = other._size;
  74. this->_capacity = other._capacity;
  75. this->_resizeFactor = other._resizeFactor;
  76. return *this;
  77. }
  78.  
  79. int& Vector::operator[](int n) const
  80. {
  81. if (n >= this->_size)
  82. {
  83. std::cout << "n bigger then elements size" << std::endl;
  84. return (this->_elements[0]);
  85. }
  86. else
  87. {
  88. return(this->_elements[n]);
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement