Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // Вектор целых чисел.
- class MyVector {
- public:
- int operator[](int i) { return buffer[i]; }
- void PushBack(int x) {
- if (size == capacity) Grow();
- buffer[size++] = x;
- }
- int Size() { return size; }
- private:
- int size = 0;
- int capacity = 0;
- int* buffer = nullptr;
- void Grow() {
- int new_capacity = max(capacity * 2, 1);
- int* new_buffer = new int[new_capacity];
- for (int i = 0; i < size; ++i) new_buffer[i] = buffer[i];
- delete[] buffer;
- buffer = new_buffer;
- capacity = new_capacity;
- }
- };
- int main() {
- MyVector v;
- v.PushBack(5);
- v.PushBack(10);
- v.PushBack(11);
- v.PushBack(15);
- v.PushBack(17);
- for (int i = 0; i < v.Size(); ++i)
- cout << v[i] << " ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement