Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Vector
- {
- int *data = nullptr;
- unsigned size = 0;
- unsigned capacity = 0;
- bool insert(int n, int data)
- {
- if (n > size && n >= 0)
- {
- return false;
- }
- if (size == capacity) {
- int *temp = (int*)realloc(this->data, (capacity + capacity / 2) * sizeof(int));
- if (temp == nullptr)
- return false;
- this->data = temp;
- capacity +=capacity / 2;
- }
- for (int i = size - 1; i >= n; --i)
- {
- this->data[i + 1] = this->data[i];
- }
- this->data[n] = data;
- ++size;
- return true;
- }
- void add(int number)
- {
- if (this->capacity == 0)
- {
- this->data = new int[5];
- this->capacity = 5;
- }
- else if (this->size == this->capacity)
- {
- this->capacity = this->capacity + this->capacity / 2;
- this->data = (int*)realloc(this->data, this->capacity * sizeof(int));
- }
- this->data[this->size] = number;
- ++this->size;
- }
- void printVec()
- {
- cout << "[";
- for (int i = 0; i < this->size - 1; ++i)
- cout << this->data[i] << ", ";
- cout << this->data[this->size - 1] << "]\n";
- }
- };
- //
- //
- //
- // int* addarr(int *a, int size, int number, int index)
- // {
- // a = (int*)realloc(a, (size + 1) * sizeof(int));
- // for (int i = size-1; i >= index; --i)
- // {
- // a[i + 1] = a[i];
- // }
- // a[index] = number;
- //
- // return a;
- // }
- //
- int main()
- {
- srand(time(0));
- int x = 11;
- Vector a;
- a.add(465);
- a.add(11);
- a.insert(1, 55);
- a.printVec();
- cout << a.size << '\n';
- cout << a.capacity << '\n';
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement