vector--------vactor

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

typedef size_t size_type;
template <typename TYPE>
class vator
{


public:
    vator():first(NULL),last(first),end(first){}
    vator(size_t num,const TYPE &val):first(new TYPE[num]),last(first),end(first)
    {
        for(int i = 0 ; i < (int)num ; i++)
        {
            *last = val;
            last+=1;
        }
        end += num;
    }

    vator(const vator &from)
    {
        int m = from.end-from.first;
        first = new TYPE(m);
        last=first;
        end = first + m;
        for(int i = 0; i < m ; i++)
        {
            *last = *(from.first);
            last++;
        }
    }

    size_type size(){return last-first;}//元素个数
    size_type capacity(){return end-first;}//容器的大小
    bool empty(){return first == last;}//容器是否为空
    bool full(){return last == end+1;}//容器是否为满

    void expand()   //扩容
    {
        if(end == first)
        {
           first = new  TYPE[1];
           last = first;
           end = first;
        }
        else if(last == end)
        {
            int m = last - first;
            int mn = (end - first)*2;

            TYPE *n = new  TYPE[mn];
            memcpy(n,first,m*sizeof(TYPE));

            delete []first;
            first = n;
            last = first + m;
            end = first + mn;
        }
    }

    void push_back(const TYPE &val)//尾插
    {
        expand();
        *last++ = val;
    }


    void pop_back(){last-=1;}//尾删


    TYPE & front(){return *first;}//返回容器中第一个元素的引用

    TYPE & back(){ return *(last-1);}//返回容器中最后一个元素的引用

    void clear(){last = first;}//清空元素


    void foreh()
    {
        for(int i = 0 ; i<last-first ;i++)
        {
            cout << first[i] <<endl;

        }

    }

private:
    TYPE *first; //起始位置
    TYPE *last;   //最后一个元素的位置
    TYPE *end;  //容器的末尾位置

};




int main()
{
    vator<int>v(4,6);

    vator<int>v2(v);

    cout << v.size() << "-----" << v.capacity() <<endl;
    cout << v2.size() << "-----" << v2.capacity() <<endl;

    v2.push_back(10);

    cout << v2.size() << "-----" << v2.capacity() <<endl;

    v2.foreh();

    int &a = v2.back();

    a =19;
    v2.foreh();

    v2.clear();

     v2.foreh();
 cout << v2.size() << "-----" << v2.capacity() <<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值