c++ vector的简单实现

来源:互联网 发布:win7 网络图标 红叉 编辑:程序博客网 时间:2024/06/09 08:38

不说废话了,直接贴代码:


/* *  ccVector.h *  c++_common_codes * *  Created by xichen on 12-2-18. *  Copyright 2012 cc_team. All rights reserved. **/#ifndef CC_VECTOR_H#define CC_VECTOR_H//#include "cc_common.h"#include <exception>#include <ostream>template <class T>  // the template former shouldn't be missed.class CCQueue;template<class T>class CCVector{    friend class CCQueue<T>;public:     friend std::ostream & operator<<(std::ostream & os, const CCVector<T> & v);public:#ifdef _WINDOWS    typedef  T *iterator;    typedef  T * const_iterator;#endif    //typedef T    value;    //typedef value * iterator;public:    CCVector();    ~CCVector();    CCVector & operator=(const CCVector & v);    CCVector(const CCVector & v);public:    void    push_back(const T & value);    void    pop_back();    int    size() const;    int    length() const;    bool    empty() const;public:    T &    operator[](int index) const;public:#ifdef _WINDOWS    iteratorbegin() const;    iteratorend() const;#elseT *begin() const;    T *end() const;#endif    //const_iterator begin() const;    //const_iterator end() const;    T*data() const { return  _trueData; };private:    voidmoveDataToNext();    voidmoveDataToPrev();public:    void    clear();#ifdef _WINDOWS    void    erase(iterator it);#elsevoid    erase(T *it);#endifpublic:    void    reverse();public:    void    setReallocSizeUnit(int newReallocSizeUnit);private:    T    *_trueData;    T    *_initData;    int    _capacity;    int    _dataCnt;    // static T *_staticVar;    // it's strange that it won't cause compile error!    int    _reallocSizeUnit;private:    static const intdefaultCapacity = 128;};template<class T>void CCVector<T>::moveDataToPrev(){    --_trueData;    ++_dataCnt;}template<class T>void CCVector<T>::moveDataToNext(){    ++_trueData;    --_dataCnt;}template<class T>void CCVector<T>::reverse(){    for (int i = 0; i < _dataCnt / 2; ++i)    {T temp = _trueData[i];_trueData[i] = _trueData[_dataCnt - i - 1];_trueData[_dataCnt - i - 1] = temp;    }}template<class T>void CCVector<T>::setReallocSizeUnit( int newReallocSizeUnit ){    _reallocSizeUnit = newReallocSizeUnit;}template<class T>CCVector<T>::CCVector(){    _trueData = _initData = NULL;    _capacity = 0;    _dataCnt = 0;    _reallocSizeUnit = 8;}template<class T>CCVector<T>::~CCVector(){    delete []_initData;}template<class T>CCVector<T> & CCVector<T>::operator=(const CCVector<T> & v){    if(*this != v)    {delete []_initData;_trueData = _initData = NULL;_capacity = 0;_dataCnt = 0;#ifdef _WINDOWSCCVector<T>::iterator it;#elseT *it;#endiffor(it = v.begin(); it != v.end(); ++it){    this->push_back(*it);}    }    return *this;}template<class T>CCVector<T>::CCVector(const CCVector<T> & v){    _trueData = _initData = NULL;    _capacity = 0;    _dataCnt = 0;    if(*this != v)    {#ifdef _WINDOWSCCVector<T>::iterator it;#elseT *it;#endiffor(it = v.begin(); it != v.end(); ++it){this->push_back(*it);}    }}template<class T>void    CCVector<T>::push_back(const T & value){    if(_initData == NULL)    {int temp = CCVector::defaultCapacity / sizeof(T);if(CCVector::defaultCapacity != temp * sizeof(T)){    _capacity = (temp + 1) * sizeof(T);}else{    _capacity = CCVector::defaultCapacity;}_initData = new T[_capacity / sizeof(T)];_trueData = _initData;if(_initData == NULL)    throw std::bad_alloc();_trueData[_dataCnt] = value;++_dataCnt;return;    }    if(_capacity > _dataCnt * sizeof(T))    {_trueData[_dataCnt] = value;++_dataCnt;return;    }    intpos = _trueData - _initData;    T * temp = new T[_dataCnt + _reallocSizeUnit];    if(temp == NULL)throw std::bad_alloc();    for(int i = 0; i < _dataCnt; ++i)temp[i + pos] = _trueData[i];    delete []_initData;    _initData = temp;    _trueData = _initData + pos;    _capacity += sizeof(T) * _reallocSizeUnit;    _trueData[_dataCnt] = value;    ++_dataCnt;}template<class T>void    CCVector<T>::pop_back(){    if(_dataCnt == 0)return;    _dataCnt--;}template<class T>intCCVector<T>::size() const{    return _dataCnt;}template<class T>intCCVector<T>::length() const{    return _dataCnt;}template<class T>bool    CCVector<T>::empty() const{    return _dataCnt == 0;}template<class T>T &CCVector<T>::operator[](int index) const{    return _trueData[index];}template<class T>T *    CCVector<T>::begin() const// it's very strange that the return type can't be iterator???{    return _trueData;}template<class T>T *    CCVector<T>::end() const{    return _trueData + _dataCnt;}/*template<class T>const T * CCVector<T>::begin() const{    return _data;}template<class T>const T * CCVector<T>::end() const{    return _data + _dataCnt;}*/template<class T>void    CCVector<T>::clear(){    delete []_initData;    _trueData = _initData = NULL;    _capacity = 0;    _dataCnt = 0;}template<class T>#ifdef _WINDOWSvoidCCVector<T>::erase(iterator it)#elsevoidCCVector<T>::erase(T *it)#endif{#ifdef_WINDOWS    CCVector<T>::iterator temp;#elseT *temp;#endif    for(temp = it + 1; temp != this->end(); ++temp)    {*(temp - 1) = *temp;    }    --_dataCnt;}#endif

简单的测试代码:

void ccTestSTLBase(){#if 1// CCVectorCCVector<int> v;v.push_back(10);v.push_back(100);v.push_back(1000);cc_print_oneline(v.begin(), v.end());COUT_ENDL(v.size());v.pop_back();cc_print_oneline(v.begin(), v.end());COUT_ENDL(v.size());#endif}

运行结果:

10 100 1000 310 100 2

注:

1  cc_print_oneline的定义:

 

template <class Iter>voidcc_print_oneline(Iter begin, Iter end){    for (; begin != end; ++begin)    {std::cout << *begin << " ";    }    std::cout << std::endl;}

2   COUT_ENDL宏的定义:

#define COUT_ENDL(message)std::cout << (message) << std::endl; 



原创粉丝点击