仿C++ STL vector

通过对C++STL vector的分析,自己写了一个简单的Vector,作为学习用。


1. Vector的特点:

vector也就是顺序列表,是用类表示一维数组。

(1)随机存取

(2)在中间插入,删除操作比较麻烦,因为需要移动后面的数据。


2. Vector ADT

(1)属性

size: Vector 中有元素个数

capacity:Vector中可用空间大小(已用+未用)

elements:Vector元素

(2)操作

Vector:  构造函数

~Vector: 析构函数

= :copy

[]  :下标操作, vector[ i ]

Resize

Erase

Insert

PushBack

Delete

PopBack

GetSize

GetCapacity

Output


3. Vector类实现

/*******************************************************************************
* FILENAME		: CVector.h
* INTRODUCTION	: 
*					
* AUTHOR		: wistone
* DATE			: 2013-06-13
*
******************************************************************************/
#ifndef __CVECTOR_H__
#define __CVECTOR_H__

#include <iostream>
using namespace std;

template <typename T> 
class CVector
{
private:
	T* elements;
	size_t mi_size;									//data size
	size_t mi_capacity;								//vector capacity
public:
	explicit CVector(size_t s);
	CVector( size_t size, const T value );
	CVector( const CVector<T>& );
	~CVector(void);

	T& operator[] (const size_t i);
	const T& operator[](const size_t i) const;
	CVector& operator= ( const CVector<T>& );

	int Resize( size_t new_size );					//bigger or smaller
	int Erase(size_t size = 0);

	int Insert( const size_t i_index, const T data );//i_index = pos -1 :insert into vector[i_index]
	int PushBack( const T data );					//insert at the end of vector
	int Delete( const size_t i_index );				//delete vector[i_index]
	int PopBack();

	size_t GetSize() const;
	size_t GetCapacity() const;
	
	void Output( ostream& out ) const;

};

#endif

4. Vector实现

#include "CXVector.h"

template <typename T> 
CVector<T>::CVector( size_t s ):mi_size(0)
{
    if( s<1024 )
    {
        mi_capacity = 1024;
    }
    else
        mi_capacity = s;

    m_elements = new T[mi_capacity];
    if( !m_elements )
    {
        cerr<<"ERROR! <CVector()> Allocate Memeroy Error.\n";
        exit(1);
    }
    
}

template <typename T>
inline CVector<T>::CVector( size_t size, const T value ):mi_size(size)
{
    if( size<1024 )
    {
        mi_capacity = 1024;
    }
    else
        mi_capacity = size;

    m_elements = new T[mi_capacity];
    if( !m_elements )
    {
        cerr<<"ERROR! <CVector()> Allocate Memeroy Error.\n";
        exit(1);
    }

    for ( int i=0; i<mi_size; i++ )
    {
        m_elements[i] = value;
    }
}

template <typename T>
inline CVector<T>::CVector( const CVector<T>& vec )
    :mi_size(vec.GetSize() ), mi_capacity( vec.GetCapacity() )
{
    m_elements = new T[mi_capacity];
    if( !m_elements )
    {
        cerr<<"ERROR! <CVector()> Allocate Memeroy Error.\n";
        exit(1);
    }

    for ( int i=0; i<mi_size; i++ )
    {
        m_elements[i] = vec[i];
    }
}

template <typename T>
inline CVector<T>::~CVector()
{
    mi_size = mi_capacity =0;
    if( m_elements )
    {
        delete[] m_elements;
    }
}
 
template <typename T> 
 inline T& CVector<T>:: operator[]( size_t i)
{
	if( i<0 || i>=mi_size )
	{
		cerr<<"ERROR! <operator[]> Index out of range.\n";
		exit(1);
		
	}
	return *(elements+i);
}

 template <typename T> 
 inline const T& CVector<T>:: operator[] (const size_t i) const
{
	if( i<0 || i>=mi_size )
	{
		cerr<<"ERROR! <operator[]> Index out of range.\n";
		exit(1);
	}
	return *(elements + i);
}

 template <typename T> 
 inline size_t CVector<T>:: GetSize() const
 {
	 return mi_size;
 }

 template<typename T>
 inline size_t CVector<T>::GetCapacity() const
 {
	 return mi_capacity;
 }

template <typename T>
inline int CVector<T>::Insert( const size_t i_index, const T data )
{
	if( i_index<0 || i_index>mi_capacity )
	{
		cerr<<"ERROR! <Insert()> Index out of range.\n";
		return XR_InvaildParam;
	}

	if( mi_size == mi_capacity )
		Resize(mi_capacity<<1);

	size_t i = mi_size;
	while (i > i_index)
	{
		elements[i] = elements[i-1];
		i--;
	}
	elements[i_index] = data;
	mi_size++;

	return XR_OK;
}

template <typename T>
inline int CVector<T>::PushBack(const T data)
{
	return Insert(mi_size, data);
}

template <typename T>
inline int CVector<T>::Delete(const size_t i_index)
{
	if( i_index<0 || i_index>=mi_size )
	{
		cerr<<"ERROR! <Delete()> Index out of range.\n";
		return XR_InvaildParam;
	}

	for (size_t i = i_index; i<mi_size-1; i++)
	{
		elements[i] = elements[i+1];
	}
	
	mi_size--;
	return XR_OK;
}

template <typename T>
inline int CVector<T>::PopBack()
{
	return Delete(mi_size-1);
}

template <typename T>
inline int CVector<T>::Resize(size_t size)
{
	T* tmp = new T[size];
	if( !tmp )
	{
		cerr<<"ERROR! Allocate Memeroy Error.\n";
		return XR_MemError;
	}
	if( mi_size > size )
		mi_capacity =  mi_size = size;
	else
		mi_capacity = size;

	for (size_t i=0; i< size; i++)
	{
		tmp[i] = elements[i];
	}

	if( elements ) 
		delete[] elements;

	elements = tmp;

	return XR_OK;
}

template <typename T>
inline void CVector<T>::Output(std::ostream &out) const
{
	for( size_t i = 0; i< mi_size; i++ )
	{
		out<<elements[i]<<"\t";
	}
	out<<endl;
}

template <typename T>
inline int CVector<T>::Erase(size_t size)
{
	if(elements)
		delete[] elements;
	if (size)
	{
		mi_size = mi_capacity = size;
	}
	else
	{
		mi_size = 0;
		mi_capacity = 1024;
	}
	
	elements = new T[mi_capacity];
	if( !elements )
	{
		cerr<<"ERROR! <CVector()> Allocate Memeroy Error.\n";
		return XR_MemError;
	}

	return XR_OK;
}

注:代码我之经过简单测试,如果你发现什么问题请与本人联系


5. 测试程序

#include <iostream>
#include <cstdio>
#include "XData.h"
#include "CXVector.h"
#include "CXVector.cpp"

using namespace std;

int main(int argc, char **argv)
{
    CVector<int> vec1(10,2);
    CVector<int> vec2(10,3);
    CVector<int> vec3(100,21);

    CVector<char> vec_txt(1000,'a');

    cout<<"Vec1:\n\tsize: "<<vec1.GetSize()<<"\tcapacity: "<<vec1.GetCapacity()<<"\n";

    for (unsigned int i=0; i<100; i++)
    {
        vec1.PushBack(i);
    }
    vec1.Insert(10,999);
    vec1.Delete(34);
    vec1.Delete(34);
    vec1.Delete(34);

    cout<<"Vec1:\n\tsize: "<<vec1.GetSize()<<"\tcapacity: "<<vec1.GetCapacity()<<"\n";
    cout<<"Vec1 data: \n";
    vec1.Output(cout);

    cout<<"Vec2 data: \n";
    vec2.Output(cout);

    vec_txt.Resize(100);
    cout<<"vec_txt:\n\tsize: "<<vec_txt.GetSize()<<"\tcapacity: "<<vec_txt.GetCapacity()<<"\n";
    cout<<"Vec txt data: \n";
    vec_txt.Output(cout);


    return 0;
}
运行结果:

Vec1:
        size: 10        capacity: 1024
Vec1:
        size: 108       capacity: 1024
Vec1 data:
2       2       2       2       2       2       2       2       2       2
999     0       1       2       3       4       5       6       7       8
9       10      11      12      13      14      15      16      17      18
19      20      21      22      26      27      28      29      30      31
32      33      34      35      36      37      38      39      40      41
42      43      44      45      46      47      48      49      50      51
52      53      54      55      56      57      58      59      60      61
62      63      64      65      66      67      68      69      70      71
72      73      74      75      76      77      78      79      80      81
82      83      84      85      86      87      88      89      90      91
92      93      94      95      96      97      98      99
Vec2 data:
3       3       3       3       3       3       3       3       3       3

vec_txt:
        size: 100       capacity: 100
Vec txt data:
a       a       a       a       a       a       a       a       a       a
a       a       a       a       a       a       a       a       a       a
a       a       a       a       a       a       a       a       a       a
a       a       a       a       a       a       a       a       a       a
a       a       a       a       a       a       a       a       a       a
a       a       a       a       a       a       a       a       a       a
a       a       a       a       a       a       a       a       a       a
a       a       a       a       a       a       a       a       a       a
a       a       a       a       a       a       a       a       a       a
a       a       a       a       a       a       a       a       a       a

Press any key to continue . . .

6. 参考文献

《C++程序设计语言》

《数据结构与算法分析》

《数据结构(STL框架)》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值