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 _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
    iterator	begin() const;
    iterator	end() const;
#else
	T *			begin() const;
    T *			end() const;
#endif
    //const_iterator begin() const;
    //const_iterator end() const;
    T	*	data() const { return  _trueData; };

private:
    void	moveDataToNext();
    void	moveDataToPrev();

public:
    void    clear();
#ifdef _WINDOWS
    void    erase(iterator it);
#else
	void    erase(T *it);
#endif
	
public:
    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 int	defaultCapacity = 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 _WINDOWS
	CCVector<T>::iterator it;
#else
	T *it;
#endif
	for(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 _WINDOWS
		CCVector<T>::iterator it;
#else
		T *it;
#endif
		for(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;
    }

    int	pos = _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>
int	CCVector<T>::size() const
{
    return _dataCnt;
}

template<class T>
int	CCVector<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 _WINDOWS
void	CCVector<T>::erase(iterator it)
#else
void	CCVector<T>::erase(T *it)
#endif
{
#ifdef	_WINDOWS
    CCVector<T>::iterator temp;
#else
	T *temp;
#endif
    for(temp = it + 1; temp != this->end(); ++temp)
    {
		*(temp - 1) = *temp;
    }
    --_dataCnt;
}

#endif


简单的测试代码:

void ccTestSTLBase()
{
#if 1	// CCVector
	CCVector<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 
3
10 100 
2


注:

1  cc_print_oneline的定义:

template <class Iter>
void	cc_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; 


微风不燥,阳光正好,你就像风一样经过这里,愿你停留的片刻温暖舒心。

我是程序员小迷(致力于C、C++、Java、Kotlin、Android、Shell、JavaScript、TypeScript、Python等编程技术的技巧经验分享),若作品对您有帮助,请关注、分享、点赞、收藏、在看、喜欢,您的支持是我们为您提供帮助的最大动力。

欢迎关注。助您在编程路上越走越好!

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值