Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template<typename T>
- class Vector {
- public:
- Vector() : size_(0), capacity_(4), arr_(new T[4]) {}
- explicit Vector(const int vector_capacity) : size_(0), capacity_(vector_capacity), arr_(new T[vector_capacity]) {}
- Vector(const initializer_list<T>& list) : size_(list.size()), capacity_(list.size()), arr_(new T[list.size()]) {
- int i = 0;
- for (const T& element : list) {
- arr_[i] = element;
- i++;
- }
- }
- Vector(const Vector& other) : size_(other.size_), capacity_(other.capacity_), arr_(new T[capacity_]) {
- for (int i = 0; i < size_; i++) {
- arr_[i] = other.arr_[i];
- }
- total_number_elements_ += size_;
- } // конструктор копирования
- Vector& operator=(const Vector& other) {
- if (&other != this) { // проверка на самоприсваивание
- total_number_elements_ += other.size_ - size_; // общее количество увеличивается (уменьшается) на разность элементов
- size_ = other.size_;
- capacity_ = other.capacity_;
- delete[] arr_;
- arr_ = new T[capacity_];
- for (int i = 0; i < size_; i++) {
- arr_[i] = other.arr_[i];
- }
- }
- return *this;
- } // копирующий оператор присваивания =
- Vector(Vector&& other) : size_(other.size_), capacity_(other.capacity_), arr_(other.arr_) {
- other.arr_ = nullptr;
- other.size_ = 0;
- other.capacity_ = 0;
- } // конструктор перемещения
- Vector& operator=(Vector&& other) {
- if (&other != this) {
- total_number_elements_ += other.size_ - size_; // общее количество увеличивается (уменьшается) на разность элементов
- size_ = other.size_;
- capacity_ = other.capacity_;
- delete[] arr_;
- arr_ = other.arr_;
- other.arr_ = nullptr;
- other.size_ = 0;
- other.capacity_ = 0;
- }
- return *this;
- } // перемещающий оператор присваивания =
- void push_back(const T value) {
- check_capacity();
- arr_[size_] = value;
- size_++;
- total_number_elements_++;
- }
- void pop_back() {
- if (size_ > 0) {
- size_--;
- }
- total_number_elements_--;
- }
- void insert(const int index, const T value) {
- if (index < 0 || index > size_) {
- cout << "Некорректный индекс" << 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 << "Некорректный индекс" << endl;
- return;
- }
- for (int i = index; i < size_ - 1; i++) {
- arr_[i] = arr_[i + 1];
- }
- size_--;
- total_number_elements_--;
- }
- T& operator[](const int index) {
- if (index < 0 || index >= size_) {
- cout << "Некорректный индекс" << endl;
- }
- return arr_[index];
- }
- T operator[](const int index) const {
- if (index < 0 || index >= size_) {
- cout << "Некорректный индекс" << endl;
- }
- return arr_[index];
- }
- int size() const {
- return size_;
- }
- int capacity() const {
- return capacity_;
- }
- bool operator==(const Vector& right) const {
- if (right.size_ != size_) {
- return false;
- }
- for (int i = 0; i < size_; i++) {
- if (arr_[i] != right.arr_[i])
- return false;
- }
- return true;
- }
- bool operator!=(const Vector& right) const {
- return !(*this == right);
- }
- void print() const {
- for (int i = 0; i < size_; i++) {
- cout << arr_[i] << " ";
- }
- cout << endl;
- }
- static int get_total_number_elements() {
- return total_number_elements_;
- }
- ~Vector() {
- delete[] arr_;
- total_number_elements_ -= size_;
- }
- private:
- void check_capacity() {
- if (size_ == capacity_) {
- T* temp = new T[capacity_ * 2];
- for (int i = 0; i < size_; i++) {
- temp[i] = arr_[i];
- }
- delete[] arr_;
- arr_ = temp;
- capacity_ *= 2;
- }
- }
- int size_; // текущее количество элементов
- int capacity_; // емкость хранилища
- T* arr_; // хранилище
- static int total_number_elements_; // общее количество элементов по всем векторам
- };
- int Vector<int>::total_number_elements_ = 0;
- int Vector<string>::total_number_elements_ = 0;
- int main() {
- setlocale(LC_ALL, "ru");
- Vector<int> vec1;
- vec1.push_back(10);
- vec1.push_back(15);
- vec1.push_back(20);
- vec1.push_back(25);
- vec1.push_back(22);
- vec1.print();
- vec1.pop_back();
- vec1.print();
- vec1.insert(2, 44);
- vec1.print();
- vec1.erase(2);
- vec1.print();
- vec1[0] = 33;
- cout << vec1[3] << endl;
- vec1.print();
- cout << vec1.size() << endl;
- cout << vec1.capacity() << endl;
- cout << Vector<int>::get_total_number_elements() << endl;
- const Vector<int> vec2(vec1);
- vec2.print();
- cout << vec2.size() << endl;
- cout << vec2.capacity() << endl;
- Vector<int> vec3;
- vec3 = vec1;
- cout << Vector<int>::get_total_number_elements() << endl;
- cout << (vec1 == vec2) << endl;
- cout << (vec1 != vec2) << endl;
- Vector<string> vec4{ "один", "два", "три" };
- vec4.print();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement