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);
- void operator=(const SimpleVector& other);
- void 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>
- void 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());
- }
- }
- template <typename T>
- void 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;
- }
- }
- 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()
- {
- SimpleVector<int> source (5);
- SimpleVector<int> source2 (5);
- cout << &source << ' ' << &source2 << endl;
- for(size_t i=0; i < source.Size(); ++i)
- {
- source[i]=i;
- }
- cout<<source<<endl;
- source = source;
- cout<<source<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement