Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- class Vector {
- public:
- Vector() {
- arr = new int[4];
- size = 0;
- capacity = 4;
- }
- void push_back(const int value) {
- check_capacity();
- arr[size] = value;
- size++;
- }
- void insert(const int index, const int value) {
- if (index < 0 || index >= size) {
- cout << "Invalid index" << endl;
- return;
- }
- check_capacity();
- for (int i = size; i > index; i--) {
- arr[i] = arr[i - 1];
- }
- arr[index] = value;
- size++;
- }
- void erase(const int index) {
- }
- void pop_back() {
- size--;
- }
- void print() {
- for (int i = 0; i < size; i++) {
- cout << arr[i] << " ";
- }
- cout << endl;
- }
- private:
- int* arr; // массив для хранения элементов
- int size; // текущее количество элементов в массиве
- int capacity; // вместимость (емкость) массива
- void check_capacity() {
- if (size == capacity) {
- int* temp = new int[capacity * 2];
- for (int i = 0; i < size; i++) {
- temp[i] = arr[i];
- }
- delete[] arr;
- arr = temp;
- capacity *= 2;
- }
- }
- };
- void Print(vector<int>& vec) {
- for (int a : vec) {
- cout << a << " ";
- }
- cout << endl;
- }
- int main() {
- Vector vec2;
- vec2.print();
- vec2.push_back(35);
- vec2.print();
- for (int i = 0; i < 10; i++) {
- vec2.push_back(i + 1);
- }
- vec2.print();
- vec2.pop_back();
- vec2.print();
- vec2.insert(2, 55);
- vec2.print();
- }
Advertisement
Add Comment
Please, Sign In to add comment