Chapter16:实例Vector

Posted wangyanphp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Chapter16:实例Vector相关的知识,希望对你有一定的参考价值。

template<typename Item>
class Vector
{
public:
    /**
     *默认构造函数
     */
    Vector(){}
    /**
     *拷贝控制
     */
    Vector(const Vector &s)
    {
        auto newdata = alloc_n_copy(s.begin(), s.end());
        elements = newdata.first;
        first_free = cap = newdata.second;
    }

    Vector &operator=(const Vector &rhs)
    {
        auto newdata = alloc_n_copy(rhs.begin(), rhs.end());
        free();
        elements = newdata.first;
        first_free = cap = newdata.second;
        return *this;
    }

    Vector(Vector &&s) noexcept
        :elements(s.elements),first_free(s.first_free),cap(s.cap)
    {
        s.elements = s.first_free = s.cap = nullptr;
    }

    Vector &operator=(Vector &&rhs) noexcept
    {
        if (this != rhs)
            free();
        
        //take over resources from rhs
        elements = rhs.elements;
        first_free = rhs.first_free;
        cap = rhs.cap;

        //leave rhs in a destructible state
        rhs.elements = rhs.first_free = rhs.cap = nullptr;

        return *this;
    }

    ~Vector() noexcept 
    { 
        free(); 
    }

    /**
     *list assignment
     */
    Vector &operator=(std::initializer_list<Item> il)
    {
        auto newdata = alloc_n_copy(il.begin(), il.end());
        free();
        elements = newdata.first;
        first_free = cap = data.second;
    }

    /**
     *add item to vector
     */
    void push_back(const Item &s)
    {
        chk_n_alloc();
        alloc.construct(first_free++, s);
    }

    void push_back(Item &&s)
    {
        chk_n_alloc();
        alloc.construct(first_free++, std::move(s));
    }

    template<class...Args> void emplace_back(Args&&... args)
    {
        chk_n_alloc();
        alloc.construct(first_free++, std::forward<Args>(args)...);
    }

    /**
     *size and capacity
     */
    size_t size() const { return first_free - elements; }
    size_t capacity() const { return cap - elements; }

    /**
     *item access
     */
    Item &operator[](size_t n) { return elements[n]; }
    const Item &operator[](size_t n) const { return elements[n]; }

    /**
     *iterator interface
     */
    Item* begin() const { return elements; }
    Item* end() const { return first_free; }

private:
    static std::allocator<Item> alloc;
    
    /**
     *check whether have memory,
     *if not, reallocate 
     */
    void chk_n_alloc()
    {
        if (first_free == cap)
            reallocate();
    }

    std::pair<Item*, Item*> alloc_n_copy(const Item *b, const Item *e)
    {
        auto data = alloc.allocate(e - b);
        return{ data, std::uninitialized_copy(b,e,data) };
    }
    
    /**
     *销毁元素,释放内存
     */
    void free()
    {
        //destory the older items in reverse order
        for (auto p = first_free; p != elements;/*empty*/)
            alloc.destroy(--p);

        //deallocate memory
        if (elements)
            alloc.deallocate(elements, cap - elements);
    }

    void reallocate()
    {
        auto newcapacity = size() ? 2 * size() : 2;

        //allocate new space
        auto first = alloc.allocate(newcapacity);
        auto dest = first;
        auto elem = elements;

        //move the elements
        for (size_t i = 0; i != size(); ++i)
            alloc.construct(dest++, std::move(*elem++));
        free();

        elements = first;
        first_free = dest;
        cap = elements + newcapacity;
    }


    Item *elements = nullptr;//指向数组首元素的地址
    Item* first_free = nullptr;//指向数组第一个空闲元素
    Item* cap = nullptr;//指向数组尾后位置的指针
};
//definition for the static data member
template<typename Item> std::allocator<Item> Vector<Item>::alloc;

 

以上是关于Chapter16:实例Vector的主要内容,如果未能解决你的问题,请参考以下文章

C++ Primer笔记16---chapter13 代码实例

零元学Expression Blend 4 - Chapter 16 用实例了解互动控制项「Button」II

Chapter16:模板

c++ primer chapter 16.4

C++ Primer笔记13---chapter12&13中的代码实例

创建片段而不从 java 代码实例化它