Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- template <typename T>
- class SimpleVector
- {
- public:
- SimpleVector() = default;
- explicit SimpleVector(size_t size);
- SimpleVector(const SimpleVector<T>& other);
- SimpleVector(SimpleVector&& other);
- SimpleVector<T>& operator=(const SimpleVector& other);
- SimpleVector<T>& operator=(SimpleVector&& other);
- ~SimpleVector();
- T& operator[](size_t index);
- T* begin();
- T* end();
- const T* begin() const;
- const T* end() const;
- size_t Size() const;
- size_t Capacity() const;
- void PushBack(const T& value);
- void PushBack(T&& value);
- private:
- T* data = nullptr;
- size_t size = 0;
- size_t capacity = 0;
- };
- template <typename T>
- inline SimpleVector<T>::SimpleVector(size_t size)
- : data(new T[size]), size(size), capacity(size) { cout << this << endl; }
- template <typename T>
- SimpleVector<T>::SimpleVector(const SimpleVector<T>& other)
- : data(new T[other.capacity]), size(other.size), capacity(other.capacity)
- {
- copy(other.begin(), other.end(), begin());
- }
- template <typename T>
- SimpleVector<T>::SimpleVector(SimpleVector<T>&& other)
- : data(other.data), size(other.size), capacity(other.capacity)
- {
- other.data = nullptr;
- other.size = other.capacity = 0;
- }
- template <typename T>
- SimpleVector<T>& SimpleVector<T>::operator=(const SimpleVector<T>& other)
- {
- if(this!=&other)
- {
- delete[] data;
- data = new T[other.capacity];
- size = other.size;
- capacity = other.capacity;
- copy(other.begin(), other.end(), begin());
- }
- return *this;
- }
- template <typename T>
- SimpleVector<T>& SimpleVector<T>::operator=(SimpleVector<T>&& other)
- {
- if(this!=&other)
- {
- delete[] data;
- data = other.data;
- size = other.size;
- capacity = other.capacity;
- other.data = nullptr;
- other.size = other.capacity = 0;
- }
- return *this;
- }
- template <typename T>
- void SimpleVector<T>::PushBack(const T& value)
- {
- if (size >= capacity)
- {
- auto new_cap = (capacity == 0) ? 1 : 2 * capacity;
- auto new_data = new T[new_cap];
- copy(begin(), end(), new_data);
- delete[] data;
- data = new_data;
- capacity = new_cap;
- }
- data[size++] = value;
- }
- template <typename T>
- inline size_t SimpleVector<T>::Capacity()const
- {
- return capacity;
- }
- template <typename T>
- inline SimpleVector<T>::~SimpleVector()
- {
- delete[] data;
- }
- template <typename T>
- inline T& SimpleVector<T>::operator[](size_t index)
- {
- return data[index];
- }
- template<typename T>
- T* SimpleVector<T>::begin()
- {
- return data;
- }
- template<typename T>
- T* SimpleVector<T>::end()
- {
- return data + size;
- }
- template<typename T>
- const T* SimpleVector<T>::begin() const
- {
- return data;
- }
- template<typename T>
- const T* SimpleVector<T>::end() const
- {
- return data + size;
- }
- template <typename T>
- inline size_t SimpleVector<T>::Size() const
- {
- return size;
- }
- template <typename T>
- ostream & operator << (ostream& os, SimpleVector<T>& rhs)
- {
- os<<"Size = "<<rhs.Size()<<" Items:";
- for(const auto & x:rhs)
- {
- os<<' '<<x;
- }
- return os;
- }
- int main()
- {
- //int a,b,c,d,e;
- //a = (b = (c = (d = (e = 0))));
- SimpleVector<int> e(5);
- SimpleVector<int> a, b, c, d;
- a = b = c = d = e;
- cout << a.Size() << endl;
- // Оператор присваивания правоассоциативен
- // Чтобы работало цепное присваивание, оператор присваивания должен возвращать ссылку на себя
- // Это делается с помощью возврата *this
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement