Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <iostream>
- //#include <vector>
- //
- //using namespace std;
- //
- //class Logger
- //{
- //public:
- // Logger()
- // {
- // cout << "Default ctor\n";
- // }
- // Logger(const Logger&)
- // {
- // cout << "Copy ctor\n";
- // }
- // void operator=(const Logger&)
- // {
- // cout << "Copy assignment\n";
- // }
- //};
- //
- //int main()
- //{
- // Logger source;
- // Logger target = source;
- //
- // vector<Logger> loggers;
- // loggers.push_back(target);
- //
- // source = target;
- //}
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
- template <typename T>
- class SimpleVector
- {
- public:
- SimpleVector() = default;
- explicit SimpleVector(size_t size);
- SimpleVector(const SimpleVector<T>& other);
- //rvalue reference ведет себя так же как ссылка, но позволяет принимать временный объект
- 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);
- private:
- T* data = nullptr;
- size_t size = 0;
- size_t capacity = 0;
- };
- template <typename T>
- SimpleVector<T>::SimpleVector(size_t size) : data(new T[size]), size(size), capacity(size)
- {
- }
- 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&& 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& 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&& other)
- {
- delete[]data;
- data = other.data;
- size = other.size;
- capacity = other.capacity;
- copy(other.begin(), other.end(), begin());
- other.data = nullptr;
- other.size = other.capacity = 0;
- }
- template <typename T>
- SimpleVector<T>::~SimpleVector()
- {
- delete[]data;
- }
- template <typename T>
- T& SimpleVector<T>::operator[](size_t index)
- {
- return data[index];
- }
- template <typename T>
- size_t SimpleVector<T>::Size() const
- {
- return size;
- }
- template <typename T>
- size_t SimpleVector<T>::Capacity() const
- {
- return capacity;
- }
- 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>
- T* SimpleVector<T>::begin()
- {
- return data;
- }
- template <typename T>
- const T* SimpleVector<T>::begin() const
- {
- return data;
- }
- template <typename T>
- T* SimpleVector<T>::end()
- {
- return data + size;
- }
- template <typename T>
- const T* SimpleVector<T>::end() const
- {
- return data + size;
- }
- int main()
- {
- /* SimpleVector<int> source(1);
- source[0] = 8;
- SimpleVector<int> target= source;
- cout << source[0] << " " << target[0] << endl;*/
- /*SimpleVector<int> source(1);
- SimpleVector<int> target = move(source);
- cout << source.Size() << " " << target.Size() << endl;*/
- SimpleVector<int> source(1);
- SimpleVector<int> target(1);
- target = move(source);
- cout << source.Size() << " " << target.Size() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement