c++ vector的简单实现

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

/* * 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 _WINDOWStypedef T *iterator;typedef T * const_iterator;#endif//typedef Tvalue;//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();intsize() const;intlength() const;bool empty() const;public:T &operator[](int index) const;public:#ifdef _WINDOWSiteratorbegin() 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 _WINDOWSvoid 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_WINDOWSCCVector<T>::iterator temp;#elseT *temp;#endiffor(temp = it + 1; temp != this->end(); ++temp){*(temp – 1) = *temp;}–_dataCnt;}#endif莫找借口失败,只找理由成功。(不为失败找理由,要为成功找方法

c++ vector的简单实现

相关文章:

你感兴趣的文章:

标签云: