Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Vector{
  4.  
  5. public:
  6.  
  7. const int _dc = 1;
  8.  
  9. int _capacity;
  10.  
  11. int _size;
  12.  
  13. int *_data;
  14.  
  15. Vector()
  16. {
  17. _capacity = _dc;
  18.  
  19. _size = 0;
  20.  
  21. _data = new int[_capacity];
  22. }
  23.  
  24. void push(int key)
  25. {
  26. if(_capacity == _size)
  27. {
  28. _capacity += _dc;
  29.  
  30. int *_temp = new int[_capacity];
  31.  
  32. memcpy(_temp, _data, sizeof(int) * _size);
  33.  
  34. delete []_data;
  35.  
  36. _data = _temp;
  37. }
  38. _data[_size] = key;
  39.  
  40. _size++;
  41. }
  42.  
  43. class iterator{
  44.  
  45. public:
  46.  
  47. int *current;
  48.  
  49. iterator(int *temp)
  50. {
  51. current = temp;
  52. }
  53.  
  54. bool operator <=(iterator other)
  55. {
  56. return current <= other.current;
  57. }
  58.  
  59. bool operator >=(iterator other)
  60. {
  61. return current >= other.current;
  62. }
  63.  
  64. iterator& operator ++()
  65. {
  66. this->current++;
  67.  
  68. return *this;
  69. }
  70.  
  71. iterator operator ++(int)
  72. {
  73. iterator i = *this;
  74.  
  75. current = ++current;
  76.  
  77. return i;
  78. }
  79.  
  80. iterator& operator --()
  81. {
  82. this->current--;
  83.  
  84. return *this;
  85. }
  86.  
  87. iterator operator --(int)
  88. {
  89. iterator i = *this;
  90.  
  91. current = --current;
  92.  
  93. return i;
  94. }
  95.  
  96. int operator *()
  97. {
  98. return *current;
  99. }
  100. };
  101.  
  102. iterator begin()
  103. {
  104. return iterator(_data);
  105. }
  106.  
  107. iterator end()
  108. {
  109. return iterator(_data + _size - 1);
  110. }
  111. };
  112.  
  113. int main()
  114. {
  115. Vector vector;
  116.  
  117. vector.push(20);
  118.  
  119. vector.push(50);
  120.  
  121. vector.push(80);
  122.  
  123. vector.push(90);
  124.  
  125. vector.push(15);
  126.  
  127. for(Vector::iterator i = vector.begin(); i <= vector.end(); ++i)
  128. {
  129. if ((*i) < 30){
  130. std::cout << "30" << " ";
  131. }
  132.  
  133.  
  134. else if (((*i)>=30)&&((*i)<=60)){
  135. std::cout << "40" << " ";
  136. }
  137.  
  138.  
  139. else std::cout << "60" <<" ";
  140.  
  141. }
  142. return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement