ulfben icon

Container 2020 day 1

ulfben | PRO | 11/23/20 09:11:20 AM UTC (Edited) | 0 ⭐ | 1553 👁️ | Never ⏰ | []
C++ |

1.49 KB

|

None

|

0 👍

/

0 👎

#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);
    }
}

Comments

  • Yuizcko icon
    05/18/26 01:00:03 PM UTC
    text |

    0 B

    |

    0 👍

    /

    0 👎

    Best t33n active cpx link daily updates!
     Copy and paste the link in a new browser tab and then hit enter!
     https://crazyporn.xxx/members/157354/?asgtbndr=1&play=true&spon=adworld&zone=400