C++ Vector 顺序表

Vector.h

#include<iostream>
#include<assert.h>
using namespace std;

typedef int DataType;

class Vector
{
public:
    Vector()//构造函数
        : first(NULL)
        , finish(NULL)
        , end(NULL)
    {}

    size_t size() const//当前已用容量
    {
        return finish - first;
    }

    size_t capacity() const//最大容量
    {
        return end - first;
    }

    void reverse(size_t n)//预留空间
    {
        if (n > capacity())
        {
            expand(n);
        }
    }

    void resize(size_t n, DataType value)//设置分配空间,并对多开空间赋值
    {
        if (n < size())//缩小
        {
            finish = first + n;
        }
        else
        {
            if (n > capacity())
            {
                expand(n);//扩容

            }
            size_t index = n - size();
            while (--index)
            {
                *finish = value;
                finish++;
            }

        }
    }


    Vector(const Vector& v)//拷贝构造   v1(v2)
                           //为避免浅拷贝,需用深拷贝另开空间
    {
        DataType *tmp = new DataType[v.size()];
        first = tmp;
        finish = first + v.size();
        end = first + v.size();
        memcpy(first, v.first, sizeof(DataType)*v.size());
    }

    //v1=v2=v3;
    Vector& operator=(const Vector& v)//赋值运算符重载
    {
        if (this != &v)//传统写法
        {
            delete[] first;
            DataType *tmp = new DataType[sizeof(DataType)*v.size()];
            first = tmp;
            finish = first + v.size();
            end = first + v.capacity();
            memcpy(first, v.first, sizeof(DataType)*v.size());
        }

        //Vector tmp(v);//现代写法
        //swap(first, tmp.first);
        //swap(finish, tmp.finish);
        //swap(end, tmp.end);

        return *this;
    }

    int& operator[](size_t pos)
    {
        return first[pos];
    }

    ~Vector()//析构函数
    {
        if (first)
        {
            delete[] first;
            first = finish = end = NULL;
        }

    }


    void Pushback(DataType value)//尾插
    {
        if (finish == end)
        {
            size_t newcapacity = capacity() > 0 ? (2 * capacity()) : 5;
            expand(newcapacity);
        }
        *finish = value;
        finish++;
    }

    void Popback()//尾删
    {
        assert(finish > first);
        finish--;
    }


    void Insert(size_t pos, DataType value)//任意位置插入
    {
        assert(pos >= 0 && pos < size());
        int size = (int)(this->size());
        if (finish == end)
        {
            expand(2 * capacity());
        }
        for (int i = size - 1;i >= (int)pos;)
        {
            first[i + 1] = first[i];
            i--;
        }
        first[pos] = value;
        finish++;
    }


    void Erase(size_t pos)//任意位置删除
    {
        assert(pos >= 0 && pos < size());
        assert(finish > first);
        int size = (int)this->size();
        for (int i = (int)pos;i < size;i++)
        {
            first[i] = first[i + 1];
        }
        finish--;
    }


    DataType* find(DataType value)//查找
    {
        for (size_t i = 0;i < size();i++)
        {
            if (first[i] == value)
            {
                return &(first[i]);
            }
        }
        cout << "没有找到" << endl;
        return NULL;
    }

private:
    void expand(size_t n)//增容
    {
        DataType *tmp = new DataType[sizeof(DataType)*n];
        memcpy(tmp, first, sizeof(DataType)*size());
        size_t size = finish - first;
        size_t capacity = end - first;
        delete[] first;
        first = tmp;
        finish = first + size;
        end = first + n;
    }
private:
    DataType *first;
    DataType *finish;
    DataType *end;
};

test.cpp

#include"Vector.h"



int main()
{

    Vector v1;
    v1.reverse(5);
    v1.Pushback(1);
    v1.Pushback(2);
    v1.Pushback(3);
    v1.Pushback(4);
    v1.Pushback(5);
    v1.Pushback(6);
    v1.Popback();
    v1.Insert(3, 0);
    v1.Erase(1);

    for (size_t i = 0;i < v1.size();i++)
    {
        cout << v1[i] << "  ";

    }
    cout << endl;

    cout << *(v1.find(3)) << endl;

    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Ferlan/article/details/81384469