1. 程式人生 > >使用Vector模擬實現STL中的stack

使用Vector模擬實現STL中的stack

stack 介紹

棧是一種容器介面卡,特別為後入先出而設計的一種(LIFO ),那種資料被插入,然後再容器末端取出

棧實現了容器介面卡,這是用了一個封裝了的類作為他的特定容器,提供了一組成員函式去訪問他的元素,元素從特定的容器,也就是堆疊的頭取出袁術。

這個基礎的容器可能是任何標準的容器類,和一些其他特殊設計的模板類,唯一的要求就是要支援一下的操作
實現C++ STL,棧有兩個引數。

template < class T, class Container = deque > class stack;

引數示意:

T: 元素型別
Container: 被用於儲存和訪問元素的的型別

成員函式:

(1)stack::stack

stack ( const Container& ctnr = Container() );
用於構造一個棧介面卡物件
(2)stack::empty

bool empty ( ) const;
判斷是否為空
(3)stack::pop

void pop ( );
在棧的頂部移除元素
(4)stack::push

void push ( const T& x );
在棧頂新增元素
(5)stack::size

size_type size ( ) const;
計算棧物件元素個數
(6)stack::top

value_type& top ( );
const value_type& top ( ) const;
返回棧頂元素

#pragma once

#include<stdio.h>
#include<deque>
#include<iostream>
using namespace std;

template<class T,class Con = deque<T> >
class Stack
{
public:
    typedef size_t SizeType;
    typedef T ValueType;

public
: Stack() //用於構造一個棧介面卡物件 {} bool Empty()const //判斷是否為空 { return _con.empty(); } SizeType Size()const //計算棧物件元素個數 { return _con.size(); } void Pop() //在棧的頂部移除元素 { _con.pop_back(); } void Push(const ValueType data) //在棧頂新增元素 { _con.push_back(data); } ValueType& Top() //返回棧頂元素 { return _con.back(); } const ValueType& Top()const { return _con.back(); } private: Con _con; };

用vector實現:

#include <iostream>
using namespace std;
#include <assert.h>
template<typename T>
class Vector
{
public:
    typedef T valueType;
    typedef valueType* Iteator;
    typedef const valueType* const_Iterator;
    typedef valueType& Reference;
    typedef const valueType& const_Reference;
    typedef size_t size_type;

public:
    Vector()   //建構函式
        :_start(0)
        ,_finish(0)
        ,_end_of_storage(0)
    {}

    Vector(size_type n, const T& value = T())  //建構函式,構造一個裡面有n個相同值的順序表
        :_start(new T[n])
    {
        for(size_type idx=0; idx<n; ++idx)
            _start[idx] = value;
        _finish = _start+n;
        _end_of_storage = _finish;
    }

    Vector(const Vector<T> &v)   //拷貝建構函式
    {
        size_type capacity = v._end_of_storage - v._start;
        _start = new T[capacity];
        size_type size = v._finish - v._start;

        for(size_type idx=0; idx<size; ++idx)
        {
            _start[idx] = v._start[idx];
        }
        _finish = _start + size; //不能用_finish = v._finish;因為改變指向會引發錯誤
        _end_of_storage = _start + capacity;
    }

    Vector<T>& operator = (const Vector<T>& v) //賦值運算子過載
    {
        if(this != &v)
        {
            size_type capacity = v._end_of_storage - v._start;
            size_type size = v._finish - v._start;
            if(Capacity() < size)
            {
                _start = new T[capacity];
                for(size_type idx=0; idx<size; ++idx)
                {
                    _start[idx] = v._start[idx];
                }
                _finish = _start + size; //不能用_finish = v._finish;因為改變指向會引發錯誤
                _end_of_storage = _start + capacity;
            }
        }
        return *this;
    }

    ~Vector()   //解構函式
    {
        if(NULL != _start)
        {
            delete[] _start;
            _start = NULL;
            _finish = NULL;
            _end_of_storage = NULL;
        }
    }

    Iteator Begin()   //得到陣列頭的指標
    { 
        return _start;
    }

    const_Iterator Begin()const
    {
        return _start;
    }

    Iteator End()  //得到陣列的最後一個單元+1的指標
    {
        return _finish;
    }

    const_Iterator End()const
    {
        return _finish;
    }

    size_type Size()const   //當前使用資料的大小
    {
        return _finish - _start;
    }

    size_type Capacity()const   //當前vector分配的大小
    {
        return _end_of_storage - _start;
    }

    bool Empty()const  //判斷vector是否為空
    {
        return Begin() == End();
    }

    Reference operator[](size_type index)  //得到編號位置的資料
    {
        assert(index<Size());
        return _start[index];
    }

    const_Reference operator[](size_type index)const
    {
        assert(index<Size());
        return _start[index];
    }

    Reference Front()   //得到陣列頭的引用
    {
        return *Begin();
    }

    Reference Back()   //得到陣列的最後一個單元的引用
    {
        return *(End()-1);
    }

    void PushBack(const T& value)    //在陣列的最後新增一個數據
    {
        CheckCapacity();
        *_finish = value;
        ++_finish;
    }

    void PopBack()   //去掉陣列的最後一個數據 
    {
        assert(0 != Size());
        --_finish;
    }

    Iteator Insert(Iteator pos, const T& value)  // 在指標指向元素的前面插入一個元素
    {
        size_type position = pos - Begin();
        CheckCapacity();
        int count = Size() - position ;
        int i = 0;
        while(count)
        {
            _start[Size()-i] = _start[Size()-i-1];
            i++;
            count--;
        }
        *pos = value;
        _finish++;
        return &(*pos);
    }

    Iteator Erase(Iteator pos)   //刪除指標指向的資料項
    {
        size_type position = pos - Begin();
        assert(0 != Size());
        assert(position < Size());
        size_t count = Size() - position - 1;
        int i = 0;
        while(count)
        {
            _start[position+i] = _start[position+i+1]; 
            i++;
            count--;
        }
        _finish--;


        return pos;
    }

    void ReSize(size_type newSize, const T& value = T())  //重新設定該容器的大小
    {
        if(newSize < Size())
        {
            _finish -= (Size() - newSize);

        }
        else
        {
            size_t count = newSize - Size();
            while(count)
            {
                CheckCapacity();
                _start[Size()] = value;
                ++_finish;
                count--;
            }
        }   
    }

    void Assign(size_t n, const T& data)  //構造一個裡面有n個相同值的順序表
    {
        _finish = 0;
        Iteator temp = new T[n];

        for(size_type idx=0; idx<n; ++idx)
            _start[idx] = value;
        _finish = _start+n;
        _end_of_storage = _finish;
    }

    void Clear()const   //清空當前的vector
    {
        _finish = _start;
    }

private:
    void CheckCapacity()
    {
        if(_finish >= _end_of_storage)
        {
            int capacity = Capacity()*2 + 3;
            Iteator pTemp = new T[capacity];
            size_type size = _finish - _start;
            memcpy(pTemp, _start, sizeof(T)*size);
            if(_start != NULL)
            {
                delete[] _start;
            }
            _start = pTemp;
            _finish = _start + size;
            _end_of_storage = _start + capacity;
        }
    }

protected:
    Iteator _start;
    Iteator _finish;
    Iteator _end_of_storage;
};

void FunTest1()
{
    Vector<int> v1();
    Vector<int> v2(10,4);
    Vector<int> v3(v2);
    Vector<int> v4;
    v4 = v2;

    cout<<v2.Capacity()<<endl;
    cout<<v2.Size()<<endl;

    Vector<int>::Iteator it = v2.Begin();
    while(it != v2.End())
    {
        cout<<*it<<" ";
        it++;
    }
    cout<<endl;

    cout<<v4.Capacity()<<endl;
    cout<<v4.Size()<<endl;
    it = v4.Begin();
    while(it != v4.End())
    {
        cout<<*it<<" ";
        it++;
    }
    cout<<endl;
}

void FunTest2()
{
    Vector<int> v1(3, 5);
    v1.PushBack(1);
    v1.PushBack(2);
    v1.PushBack(3);
    v1.PushBack(4);
    v1.PushBack(5);
    v1.PushBack(6);


    cout<<v1.Size()<<endl;
    cout<<v1.Capacity()<<endl;

    v1.PopBack();
    v1.PopBack();
    v1.PopBack();
    cout<<v1.Size()<<endl;
    cout<<v1.Capacity()<<endl;

    Vector<int>::Iteator it = v1.Begin();
    while(it != v1.End())
    {
        cout<<*it<<" ";
        it++;
    }
    cout<<endl;
}


void FunTest3()
{
    Vector<int> v1(3, 5);
    v1.PushBack(1);
    v1.PushBack(2);
    v1.PushBack(3);
    v1.PushBack(4);
    v1.PushBack(5);
    v1.PushBack(6);

    cout<<v1.Size()<<endl;
    cout<<v1.Capacity()<<endl;
    Vector<int>::Iteator it = v1.Begin();
    while(it != v1.End())
    {
        cout<<*it<<" ";
        it++;
    }
    cout<<endl;

    v1.Erase(v1.Begin()+6);
    v1.Insert(v1.Begin(), 10);
    cout<<v1.Size()<<endl;
    cout<<v1.Capacity()<<endl;

    it = v1.Begin();
    while(it != v1.End())
    {
        cout<<*it<<" ";
        it++;
    }
    cout<<endl;

    v1.Erase(v1.Begin()+2);
    v1.Insert(v1.Begin(), 10);
    cout<<v1.Size()<<endl;
    cout<<v1.Capacity()<<endl;

    it = v1.Begin();
    while(it != v1.End())
    {
        cout<<*it<<" ";
        it++;
    }
    cout<<endl;

    v1.Insert(v1.Begin(), 10);
    v1.Erase(v1.Begin());
    cout<<v1.Size()<<endl;
    cout<<v1.Capacity()<<endl;

    it = v1.Begin();
    while(it != v1.End())
    {
        cout<<*it<<" ";
        it++;
    }
    cout<<endl;

}

void FunTest4()
{

    Vector<int> v1(3, 5);
    v1.PushBack(1);
    v1.PushBack(2);
    v1.PushBack(3);
    v1.PushBack(4);
    v1.PushBack(5);
    v1.PushBack(6);
    cout<<v1.Size()<<endl;
    cout<<v1.Capacity()<<endl;

    v1.ReSize(20,100);
    cout<<v1.Size()<<endl;
    cout<<v1.Capacity()<<endl;
}
int main()
{
    FunTest1();
    FunTest2();
    FunTest3();
    FunTest4();

    system("pause");
    return 0;
}
#pragma once

#include<stdio.h>
#include<deque>
#include<iostream>
using namespace std;

//Stack棧的模板類的實現
template<class T,class Con = Vector<T> >
class Stack
{
public:
    typedef size_t SizeType;
    typedef T ValueType;

public:
    Stack()
    {}

    //判空
    bool Empty()
    {
        return (_con.Size()==0);
    }
    void Push(const ValueType data)
    {
        _con.PushBack(data);
    }

    void Pop()
    {
        _con.PopBack();
    }

    SizeType Size()const
    {
        return _con.Size();
    }

    ValueType& Top()
    {
        return _con.Back();
    }

    const ValueType& Top()const
    {
        return _con.Back();
    }
private:
    Con _con;
};