Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <algorithm>
- #include <numeric>
- namespace API
- {
- class Container
- {
- public:
- using iterator = int*; //aliases for generic code
- friend void swap(Container& a, Container& b) noexcept;
- Container() = default;
- explicit Container(size_t count) : _count(count), _data(new int[count]){
- std::iota(begin(), end(), 0);
- };
- explicit Container(const Container& that) { //copy ctor
- _data = new int[that._count];
- _count = that._count;
- std::copy(that.begin(), that.end(), _data);
- }
- Container(Container&& that) noexcept {
- _count = that._count;
- _data = that._data;
- that._data = nullptr;
- that._count = 0;
- }
- Container& operator=(Container&& that) noexcept {
- std::swap(_data, that._data);
- std::swap(_count, that._count);
- return *this;
- }
- void swap(Container& that) noexcept {
- using std::swap;
- swap(_data, that._data);
- swap(_count, that._count);
- }
- Container& operator=(const Container& that) { // copy assignment
- delete[] _data;
- _data = new int[that._count]; //overwrites an existing pointer
- _count = that._count;
- std::copy(that.begin(), that.end(), _data);
- return *this;
- }
- ~Container() {
- delete[] _data;
- }
- iterator begin() const noexcept { return _data; }
- iterator end() const noexcept { return _data + _count; }
- private:
- int* _data = nullptr;
- size_t _count = 0;
- };
- void swap(Container& a, Container& b) noexcept {
- a.swap(b);
- }
- }
Add Comment
Please, Sign In to add comment