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) {}
- Vector(const int new_capacity) : arr_(new int[new_capacity]), size_(0), capacity_(new_capacity) {}
- Vector(const Vector& other) : arr_(new int[other.capacity_]), size_(other.size_), capacity_(other.capacity_) {
- for (int i = 0; i < size_; i++) {
- arr_[i] = other.arr_[i];
- }
- total_number_elements_ += size_;
- }
- Vector& operator=(const Vector& other) {
- size_ = other.size_;
- capacity_ = other.capacity_;
- delete[] arr_;
- arr_ = new int[capacity_];
- for (int i = 0; i < size_; i++) {
- arr_[i] = other.arr_[i];
- }
- return *this;
- }
- void push_back(const int value) {
- check_capacity_();
- arr_[size_] = value;
- size_++;
- total_number_elements_++;
- }
- 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_++;
- total_number_elements_++;
- }
- void erase(const int index) {
- if (index < 0 || index >= size_) {
- cout << "Invalid index" << endl;
- return;
- }
- for (int i = index; i < size_ - 1; i++) {
- arr_[i] = arr_[i + 1];
- }
- size_--;
- total_number_elements_--;
- }
- void pop_back() {
- size_--;
- total_number_elements_--;
- }
- void print() const {
- for (int i = 0; i < size_; i++) {
- cout << arr_[i] << " ";
- }
- cout << endl;
- }
- int size() const {
- return size_;
- }
- int capacity() const {
- return capacity_;
- }
- bool operator==(const Vector& other) const {
- if (size_ != other.size_) {
- return false;
- }
- for (int i = 0; i < size_; i++) {
- if (arr_[i] != other.arr_[i]) {
- return false;
- }
- }
- return true;
- }
- bool operator!=(const Vector& other) const {
- if (size_ != other.size_) {
- return true;
- }
- for (int i = 0; i < size_; i++) {
- if (arr_[i] != other.arr_[i]) {
- return true;
- }
- }
- return false;
- }
- int& operator[] (const int index) {
- if (index < 0 || index >= size_) {
- cout << "Invalid index" << endl;
- abort;
- }
- return arr_[index];
- }
- int& operator[] (const int index) const {
- if (index < 0 || index >= size_) {
- cout << "Invalid index" << endl;
- abort;
- }
- return arr_[index];
- }
- ~Vector() {
- delete[] arr_;
- total_number_elements_ -= size_;
- }
- static int get_total_number_elements() {
- return total_number_elements_;
- }
- private:
- int* arr_; // массив для хранения элементов
- int size_; // текущее количество элементов в массиве
- int capacity_; // вместимость (емкость) массива
- static int total_number_elements_; // общее количество элементов всех экземпляров класса Vector
- 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 Vector::total_number_elements_ = 0;
- int main() {
- setlocale(LC_ALL, "ru");
- Vector vec1;
- for (int i = 0; i < 10; i++) {
- vec1.push_back(i + 1);
- }
- vec1.print();
- vec1.pop_back();
- 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();
- vec2.erase(3);
- vec2.print();
- cout << vec2[3] << endl;
- vec2[3] = 33;
- vec2.print();
- cout << "Элементов в векторе: " << vec1.size() << " Емкость вектора: " << vec1.capacity() << endl;
- cout << "Элементов в векторе: " << vec2.size() << " Емкость вектора: " << vec2.capacity() << endl;
- Vector vec3(100);
- vec3.push_back(10);
- cout << "Элементов в векторе: " << vec3.size() << " Емкость вектора: " << vec3.capacity() << endl;
- Vector vec4(vec1);
- cout << "Элементов в векторе: " << vec4.size() << " Емкость вектора: " << vec4.capacity() << endl;
- vec1.push_back(22);
- vec1.print();
- vec4.print();
- cout << boolalpha << (vec1 == vec4) << endl;
- cout << (vec1 != vec4) << endl;
- vec1.print();
- vec2.print();
- cout << endl;
- vec1 = vec2;
- vec1.print();
- vec2.print();
- const Vector vec5 = vec1;
- cout << Vector::get_total_number_elements() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment