Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <bits/stdc++.h>
- #include <iostream>
- #include <stdexcept>
- class Vector {
- int* arr;
- static int size;
- public:
- Vector() {
- arr = new int[0];
- }
- int numar_elemente() {
- return size;
- }
- void insert(int index, int value) {
- if (index > size || index < 0)
- throw std::invalid_argument("Invalid index.");
- int newSize = size + 1;
- int* newArr = new int[newSize];
- memcpy(newArr, arr, index * sizeof(int));
- memcpy(newArr + index + 1, arr + index, (size - index) * sizeof(int));
- newArr[index] = value;
- size = newSize;
- delete[] arr;
- arr = newArr;
- }
- void remove(int index) {
- if (index > size || index < 0)
- throw std::invalid_argument("Invalid index.");
- int newSize = size - 1;
- int* newArr = new int[newSize];
- memcpy(newArr, arr, index * sizeof(int));
- memcpy(newArr + index, arr + index + 1, (size - index - 1) * sizeof(int));
- size = newSize;
- delete[] arr;
- arr = newArr;
- }
- int& operator[](int index)
- {
- if (index > (size - 1) || index < 0)
- throw std::invalid_argument("Invalid index.");
- return arr[index];
- }
- };
- int Vector::size = 0;
- int main() {
- Vector caca;
- caca.insert(0, 1);
- caca.insert(1, 2);
- caca.insert(1, 99);
- std::cout << caca[0] << std::endl;
- caca.remove(0);
- std::cout << caca[0] << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement