ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

关于 cpp 五大函数及 vector 的感知

2021-10-08 19:01:31  阅读:195  来源: 互联网

标签:Intcell objects thesize rhs vector cpp 感知 thecapacity


五大函数:

析构函数

拷贝构造函数

移动构造函数

拷贝赋值 Operator=

移动赋值 Operator=


 

析构函数

只要一个对象运行越出范围,

或经受一次 delete,

则析构函数就要被调用

 

对于 Intcell,这些操作的形式是:

~Intcell();          //析构函数
Intcell(const Intcell &rhs);        //拷贝构造函数
Intcell(Intcell &&rhs);             //移动构造函数
Intcell &operator=(const Intcell &rhs);      //拷贝函数
Intcell &operator=(Intcell &&rhs);           //移动函数

 

vector 的实现

概述:

1.vector 将(通过一个指向所分配的内存块的指针变量)保留原始的数组,数组容量以及数组当时存储在 vector 中的项数。

2.该 vector 将实现五大函数以提供为拷贝构造函数 和 operator= 的深层拷贝功能,并将提供一个析构函数以回收原始数组。此外,它还将实现 c++11的移动功能。

3.vector 将提供改变 vector 的大小(一般改成更大的数)的 resize 例程,以及 reserve 例程,后者将改变 vecotr 的容量(一般是更大的数)。这个容量通过为原始数组获取新的内存块,把老内存块复制到新内存块并回收老内存块而得以更新。

4.vector 将提供operator[] 的实现。

5.vector 将提供size ,empty ,clear ,back ,pop_back ,push_back等基本例程。

6.对于内嵌函数提供支持。

 

#include <algorithm>
template <typename object>
class vector{
    public:
    explicit vector(int initsize=0):thesize(initsize),
    thecapacity{initsize+spare_capacity}
    {
        objects = new object[thecapacity];
    }

    vector(const vector &rhs):thesize{rhs.thesize},
    thecapacity{rhs.thecapacity},objects{nullptr}{
        objects = new object[thecapacity];
        for (int k = 0; k < thesize;++k)
            objects[k] = rhs.objects[k];
    }

    vector & operator= (const vector &rhs)
    {
        vector copy = rhs;
        std::swap(*this, copy);
        return *this;
    }

    ~vector() {
        delete[] objects;
    }

    vector(vector && rhs):thesize{rhs.thesize},
    thecapacity{rhs.thecapacity},objects{rhs.objects}{
        rhs.objects = nullptr;
        rhs.thesize = 0;
        rhs.thecapacity = 0;
    }

    vector & operator=(vector && rhs){
        std::swap(thesize, rhs.thesize);
        std::swap(thecapacity, rhs.thecapacity);
        std::swap(objects, rhs.objects);

        return *this;
    }

    void resize(int newsize){
        if(newsize>thecapacity)
            reserve(newsize * 2);
        thesize = newsize;
    }

    void reserve(int newcapacity){
        if(newcapacity<thesize)
            return;
        object *newarray = new object[newcapacity];
        for (int k = 0; k < thesize;++k)
            newarray[k] = std::move(objects[k]);
        thecapacity = newcapacity;
        std::swap(objects, newarray);
        delete []newarray;
    }

    objcet & operator[](int index){
        return objects[index];
    }

    const object & operator[](int index)const{
        return objects[index];
    }

    bool empty()const{
        return size() == 0;
    }

    int size()const{
        return thesize;
    }

    int capacity()const{
        return thecapacity;
    }

    void push_back(const object &x){
        if(thesize == thecapacity)
            reserve(2 * thecapacity + 1);
        object[thesize++] = x;
    }

    void push_back(object && x){
        if(thesize == thecapacity)
        reserve(2 * thecapacity + 1);
        object[thesize++] = std::move(x);
    }

    void pop_back(){
        --thesize;
    }

    const object & back()const{
        return objects[thesize - 1];
    }

    typedef object * iterator;
    typedef const object * const_iterator;

    iterator begin(){
        reutrn &objects[0];
    }

    const_iterator begin()const{
        return &objects[0];
    }

    iterator end(){
        return &objects[size()];
    }

    const_iterator end()const{
        return &objects[size()];
    }
    
    static const int spare_capacity = 16;

    private:
        int thesize;
        int thecapacity;
        object *objects;
};

 

标签:Intcell,objects,thesize,rhs,vector,cpp,感知,thecapacity
来源: https://www.cnblogs.com/GitHubhacker/p/15381322.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有