Kiri3L

my

Nov 21st, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. //
  2. // Created by k3l on 21.11.2019.
  3. //
  4.  
  5. #include "my_vector.h"
  6. #include <exception>
  7. #include <iostream>
  8.  
  9. namespace my_std{
  10.  
  11. int min(int i, int j){
  12. return i < j ? i : j;
  13. }
  14.  
  15. my_vector::my_vector() {
  16. this->size = 0;
  17. this->alloc = 0;
  18. this->array = nullptr;
  19. }
  20.  
  21. my_vector::my_vector(int n) {
  22. this->size = 0;
  23. this->alloc = n;
  24. this->array = new int[n];
  25. }
  26.  
  27. my_vector::my_vector(int n, int x) {
  28. this->size = n;
  29. this->alloc = n;
  30. this->array = new int[n];
  31. for(int i = 0; i < n; i++){
  32. array[i] = x;
  33. }
  34. }
  35.  
  36. void my_vector::resize(int new_size) {
  37. int *temp = new int[new_size];
  38. for(int i = 0; i < min(this->size,new_size); i++){
  39. temp[i] = this->array[i];
  40. }
  41. delete[] array;
  42. this->array = temp;
  43. this->size = min(size, new_size);
  44. this->alloc = new_size;
  45. }
  46.  
  47. int& my_vector::operator[](int index) {
  48. if(index < size)
  49. return array[index];
  50. throw std::exception();
  51. }
  52.  
  53. int my_vector::get_size() {
  54. return size;
  55. }
  56.  
  57. int my_vector::pop() {
  58. return array[--size];
  59. }
  60.  
  61. void my_vector::push_back(int i) {
  62. if(alloc == size)
  63. resize((int) (alloc * 1.5));
  64. array[size++] = i;
  65. }
  66.  
  67.  
  68. my_vector::~my_vector() {
  69. delete[] array;
  70. std::cout << "I've been deleted" << std::endl;
  71. }
  72.  
  73.  
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment