C++,实现Vector类,Vector实现Stack

#include <iostream>
#include <cassert>
#include <string.h>

using namespace std;

template <class Type>
class vector{
    public:
        vector();
        vector(int i);
        ~vector(){if (ptr) delete [] ptr; isize = 0;};
        void push_back(Type v);
//        void push_back(Type& v);
        void pop_back();
        Type& operator[](int i);
        int size(){return isize;}
        int capacity(){return icapacity;}
        typedef Type* iterator;
        Type* begin();
        Type* end();
        Type& back();
        bool empty();
    private:
        Type *ptr;
        int index;
        int isize;
        int icapacity;
        static const int factor = 2;
        static const int defaultsize = 4;
};

template <class Type>
Type* vector<Type>::begin()
{
    return ptr;
}

template <class Type>
Type* vector<Type>::end()
{
    if (index)
        return ptr + index;
    else
        return NULL;
}

template <class Type>
Type& vector<Type>::back()
{
    if (index)
        return ptr[index-1];
}

template <class Type>
bool vector<Type>::empty()
{
    return !(index > 0);
}

template <class Type>
Type& vector<Type>::operator[](int i)
{
    assert(i<index);

    return ptr[i];
}

template <class Type>
void vector<Type>::pop_back()
{
    if (index <= 0)
        return;
 
    index--;
}

#if 0
template <class Type>
void vector<Type>::push_back(Type& v)
{
    int old_capa;
    Type *newptr = NULL;

    if (index < icapacity){
        ptr[index++] = v;
        isize++;
    }
    else
    {
        if (icapacity){
            old_capa = icapacity;
            icapacity += factor * defaultsize;
        }
        else{
            old_capa = 0;
            icapacity = defaultsize;
        }

        try{
            newptr = new Type[icapacity]();
        }catch(const bad_alloc &e){
            cout << e.what();
            return;
        }

        if (ptr && old_capa){
            memcpy(newptr, ptr, old_capa * sizeof(Type)); 
            delete [] ptr;
        }

        ptr = newptr;
        ptr[index++] = v;
        isize++;       
    }
}
#endif

template <class Type>
void vector<Type>::push_back(Type v)
{
    int old_capa;
    Type *newptr = NULL;

    if (index < icapacity){
        ptr[index++] = v;
        isize++;
    }
    else
    {
        if (icapacity){
            old_capa = icapacity;
            icapacity += factor * defaultsize;
        }
        else{
            old_capa = 0;
            icapacity = defaultsize;
        }

        try{
            newptr = new Type[icapacity]();
        }catch(const bad_alloc &e){
            cout << e.what();
            return;
        }

        if (ptr && old_capa){
            memcpy(newptr, ptr, old_capa * sizeof(Type)); 
            delete [] ptr;
        }

        ptr = newptr;
        ptr[index++] = v;
        isize++;       
    }
}

template <class Type>
vector<Type>::vector(int i)
{
    Type *p;

    icapacity = i * factor;
    ptr = new Type[icapacity]();
    if (ptr){
        isize = i;
        index = i; 
    }
    else{
        isize = 0;
        icapacity = 0;
        index = 0; 
    }
}

template <class Type>
vector<Type>::vector()
{
    ptr = NULL;
    isize = 0;
    icapacity = 0;
    index = 0; 
}

template <class Type>
class stack
{
    public:
        stack();
        ~stack();
        //void push(Type &v);
        void push(Type v);
        Type& pop();
        bool empty();
    private:
        vector<Type> vec;
};

template <class Type>
Type& stack<Type>::pop()
{
    Type& p = vec.back();

    vec.pop_back();

    return p;
}


template <class Type>
void stack<Type>::push(Type v)
{
    vec.push_back(v);
}

#if 0
template <class Type>
void stack<Type>::push(Type &v)
{
    vec.push_back(v);
}
#endif

template <class Type>
bool stack<Type>::empty()
{
    return vec.empty();
}

template <class Type>
stack<Type>::stack()
{
}

template <class Type>
stack<Type>::~stack()
{
    while(!vec.empty())
       vec.pop_back(); 
}


template <class Type>
class pipe
{
    public:

    private:

};

int main()
{
    int i;
    vector<int> v(10);

    stack<int> s;

    for(i=0; i<10; i++)
        v[i] = i+1;

    for(i=0; i<10; i++)
       cout << " " << v[i];
   
    cout << endl; 

    v.push_back(23);    
    v.push_back(323);    
    v.push_back(999);    
    v.push_back(i);    

    cout << v.size() << v.capacity() << endl;

    for(vector<int>::iterator it=v.begin(); it!=v.end(); ++it)
       cout << " " << *it;
   
    cout << endl; 

    while(!v.empty()){
        cout << " " << v.back();
        v.pop_back();
    }

    cout << endl; 

    s.push(10);

    s.push(11);
    s.push(i);

    cout << s.pop() << " " << s.pop() << " " << s.pop() << endl;
}


 

猜你喜欢

转载自blog.csdn.net/somyjun/article/details/81451443