Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <cassert>
- #include <initializer_list>
- #include <stdexcept>
- #include <iostream>
- using namespace std;
- template <typename Type>
- class SimpleVector {
- public:
- using Iterator = Type*;
- using ConstIterator = const Type*;
- SimpleVector() noexcept : data(nullptr), size(0), capacity(0) {}
- explicit SimpleVector(size_t size) : size(size), capacity(size) {
- data = size > 0 ? new Type[size] : nullptr;
- for (size_t i = 0; i < size; ++i) {
- data[i] = Type(); // Initialize with default value
- }
- }
- SimpleVector(size_t size, const Type& value) : size(size), capacity(size) {
- data = size > 0 ? new Type[size] : nullptr;
- for (size_t i = 0; i < size; ++i) {
- data[i] = value;
- }
- }
- SimpleVector(std::initializer_list<Type> init) : size(init.size()), capacity(init.size()) {
- data = size > 0 ? new Type[size] : nullptr;
- size_t i = 0;
- for (const auto& item : init) {
- data[i++] = item;
- }
- }
- size_t GetSize() const noexcept {
- return size;
- }
- size_t GetCapacity() const noexcept {
- return capacity;
- }
- bool IsEmpty() const noexcept {
- return size == 0;
- }
- Type& operator[](size_t index) noexcept {
- if (index >= size) {
- //throw std::out_of_range("Index out of range");
- }
- return data[index];
- }
- const Type& operator[](size_t index) const noexcept {
- if (index < size) {
- // throw std::out_of_range("Index out of range");
- }
- return data[index];
- }
- Type& At(size_t index) {
- if (index >= size) throw std::out_of_range("Index is too large!");
- return (*this)[index];
- }
- const Type& At(size_t index) const {
- if (index >= size) throw std::out_of_range("Index is too large!");
- return (*this)[index];
- }
- void Clear() noexcept {
- size = 0;
- }
- void Resize(size_t new_size) {
- if (new_size > capacity) {
- Type* new_data = new Type[new_size];
- for (size_t i = 0; i < size; ++i) {
- new_data[i] = data[i];
- }
- for (size_t i = size; i < new_size; ++i) {
- new_data[i] = Type{}; // Initialize new elements with default value
- }
- delete[] data;
- data = new_data;
- capacity = new_size;
- }
- if (new_size < size) fill(data + new_size, data + size, 0);
- size = new_size;
- }
- Iterator begin() noexcept {
- return data;
- }
- Iterator end() noexcept {
- return data + size;
- }
- ConstIterator begin() const noexcept {
- return data;
- }
- ConstIterator end() const noexcept {
- return data + size;
- }
- ConstIterator cbegin() const noexcept {
- return data;
- }
- ConstIterator cend() const noexcept {
- return data + size;
- }
- SimpleVector(const SimpleVector& other)
- : size(other.size), capacity(other.capacity)
- {
- data = other.size > 0 ? new Type[size] : nullptr;
- copy(other.data, other.data + size, data);
- }
- SimpleVector& operator=(const SimpleVector& rhs) {
- if (this == &rhs) return *this;
- SimpleVector temp(rhs);
- swap(temp);
- return *this;
- }
- // Добавляет элемент в конец вектора
- // При нехватке места увеличивает вдвое вместимость вектора
- void PushBack(const Type& item) {
- if (size == capacity) {
- auto old_size = size;
- Resize(capacity * 2);
- At(old_size) = item;
- }
- else {
- data[++size] = item;
- }
- }
- // Вставляет значение value в позицию pos.
- // Возвращает итератор на вставленное значение
- // Если перед вставкой значения вектор был заполнен полностью,
- // вместимость вектора должна увеличиться вдвое, а для вектора вместимостью 0 стать равной 1
- Iterator Insert(ConstIterator pos, const Type& value) {
- auto index = pos - data;
- if (size >= capacity) {
- Type* new_data = new Type[capacity * 2];
- for (size_t i = 0; i < size; ++i) {
- new_data[i] = data[i];
- }
- for (size_t i = size; i < capacity * 2; ++i) {
- new_data[i] = Type{}; // Initialize new elements with default value
- }
- delete[] data;
- data = new_data;
- capacity = capacity == 0 ? 1 : capacity * 2;
- }
- copy(data + index, data + size, data + index + 1);
- data[index] = value;
- ++size;
- return data + index;
- }
- // "Удаляет" последний элемент вектора. Вектор не должен быть пустым
- void PopBack() noexcept {
- if (size > 0) {
- --size;
- }
- }
- // Удаляет элемент вектора в указанной позиции
- Iterator Erase(ConstIterator pos) {
- size_t index = pos - data + 1;
- if (index >= size) {
- PopBack();
- return end();
- }
- copy(data + index, data + size, data + index - 1);
- --size;
- return data + index;
- }
- // Обменивает значение с другим вектором
- void swap(SimpleVector& other) noexcept {
- std::swap(data, other.data);
- std::swap(size, other.size);
- std::swap(capacity, other.capacity);
- }
- ~SimpleVector() {
- delete[] data;
- }
- private:
- Type* data;
- size_t size;
- size_t capacity;
- };
- template <typename Type>
- inline bool operator==(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
- // Заглушка. Напишите тело самостоятельно
- if (lhs.GetSize() != rhs.GetSize())
- {
- return false;
- }
- for (size_t i = 0; i < lhs.GetSize(); ++i)
- {
- if (lhs[i] != rhs[i])
- {
- return false;
- }
- }
- return true;
- }
- template <typename Type>
- inline bool operator!=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
- // Заглушка. Напишите тело самостоятельно
- return !(lhs == rhs);
- }
- template <typename Type>
- inline bool operator<(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
- // Заглушка. Напишите тело самостоятельно
- return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
- }
- template <typename Type>
- inline bool operator<=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
- // Заглушка. Напишите тело самостоятельно
- return !(rhs < lhs);
- }
- template <typename Type>
- inline bool operator>(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
- // Заглушка. Напишите тело самостоятельно
- return rhs < lhs;
- }
- template <typename Type>
- inline bool operator>=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
- // Заглушка. Напишите тело самостоятельно
- return !(lhs < rhs);
- }
Advertisement
Add Comment
Please, Sign In to add comment