STL基础1 vector的实现

本文的代码主要实现了vector 的
1.基本操作如: push_back(),  pop_back(), size(), back()
2.操作符的重载如: [],= 

vector的定义及实现

#include<iostream>
using namespace std;
template<typename Object>
class MyVector {
    public:
        //构造函数
        MyVector(int initSize=0):theSize(initSize),theCapacity(initSize+SPACE_CAPACITY){
            object = new Object[theCapacity];
        }
        //复制构造函数
        MyVector(const MyVector& rhs):object(NULL){
            operator=(rhs);
        }
        //析构函数
        ~MyVector() {
            delete[]object;
        }

        //操作符=重载
        const MyVector &operator=(const MyVector& rhs){
            if (this != &rhs) {
                delete[]object;
                thesize = rhs.size();
                theCapacity = rhs.capacity();

                object = new Object[theCapacity];
                for (int k = 0; k < theSize; k++) {
                    object[k] = rhs.object[k];
                }
            }
            return *this;
        }
        //重载操作符[]
        Object& operator[](int index) {
            /*
            //异常处理
            if (index < 0 || index >= theSize) {
                throw "检查取值范围,取值范围应该在0-theSize-1之间";
            }
            */
            return object[index];
        }
        const Object & operator[](int index)const {
            return object[index];
        }
        //判断是否为空
        bool empty() const{
            return thesize == 0;
        }
        //长度
        int size()const {
            return theSize;
        }
        //容量
        int capacity() const{
            return theCapacity;
        }
        //重置大小
        void resize(int newsize) {
            if (newsize>theCapacity) {
                reserve(2*newsize+1);
            }
            theSize = newsize;
        }
        //重置容量
        void reserve(int newCapacity) {
            if(newCapacity<theCapacity){
                return;
            }
            Object *oldobject = object;
            object = new Object[newCapacity];
            for (int k = 0; k < theSize; k++) {
                object[k] = oldobject[k];
            }
            delete[]oldobject;
            theCapacity = newCapacity;

        }
        void push_back(const Object& x) {
            if (theSize == theCapacity) {
                reserve(2 * theCapacity + 1);
            }
            object[theSize++] = x;
        }
        void pop_back() {
            theSize--;
        }
        const Object& back()const {
            return object[theSize - 1];
        }
        enum {SPACE_CAPACITY=16};
    private:
        int theSize;
        int theCapacity;
        Object * object;
};

vector测试

void MyVectorTest() {
    //
    MyVector<int> vector1;
    cout << "size of vector1 is : "<<vector1.size() << endl;
    for (int i = 0; i < 10; i++) {
        vector1.push_back(i);
    }
    cout << "size of vector1 is : " << vector1.size() << endl;
    for (int i = 0; i < vector1.size(); i++) {
        cout << vector1[i] << " ";
    }
    cout << endl;
    vector1.pop_back();
    cout << "size of vector1 is : " << vector1.size() << endl;
    //
    MyVector<int> vector2(3);
    cout << "size of vector2 is : "<<vector2.size() << endl;

}

测试结果

这里写图片描述

ps:实现操作符[]重载时,未做异常处理

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值