C++ vector类模板

题目描述

参考C++ vector类模板,设计实现自己的CVector向量类模板,完成下列基本功能: 构造、析构、size、push_back和下标访问,在通过下标访问越界时抛出异常。编写测试程序,利用该类模板完成一个解释器,它接受命令,执行相应操作。本题不可使用STL vector,所需内存通过动态分配获得,所需内存扩展不可频繁发生。

输入描述

输入命令有5种 :int 整数 代表开始建立整数向量 (整数为向量初始大小);string 整数 代表开始建立string向量;push对象 代表在向量尾追加对象; put 下标 对象 代表需将对象放入向量下标处;fetch 下标 代表取并打印向量下标处对象;当下标越界时抛出异常并显示invalid index:下标;quit代表本队列处理结束。每个队列以int或string开始,以quit结束;所有命令均小写。

输出描述

每个向量从建立到结束输出占一行; 各输出对象前含一个空格。

样例输入

int 10 push 100 push 200 push 50 push 300 put 0 5 put 1 20 push 400 fetch 1 fetch 12 fetch -1 fetch 20 fetch 11 quit
string 10 push hello push some push apple push box put 1 zhang put 2 yes fetch 2 push hdu fetch 1 fetch 20 fetch 12 quit

样例输出

20 50 invalid index:-1 invalid index:20 200
yes zhang invalid index:20 apple

#include <iostream>
#include<cstring>
using namespace std;
template<class T>
class myvector
{
private:
    T *arry;
    int mysize;
    int space ;
public:
    myvector(const int n)
    {
        arry=new T[n];
        mysize=n;
        space=n;
    }
    ~myvector()
    {
        delete[]arry;
        mysize=0;
        space=0;
    }
    void resize(int newspace)
    {
        if(newspace<=space) return ;
        T *temp=new T[newspace];
        for(int i=0;i<mysize;i++)
        {
           temp[i]=arry[i];
        }
        delete[]arry;
        space=newspace;
        swap(arry,temp);
    }
    void push_back(const T x)
    {
        if(mysize>=space)
        {
            resize(2*space);
        }
        arry[mysize++]=x;
    }
    int size()const 
    {
    	return mysize;
	}
    T &operator[](int index)
    {
        return arry[index];
    }
    T &at(int index)const
    {

        if(index<0||index>=mysize)
        {
            throw index;
        }
        return arry[index];
    }
};
int main()
{
        string order;
        while(cin>>order)
        {
        if(order=="int")
        {
            int n;
            cin>>n;
            myvector<int>v(n);
           
            while(cin>>order)
            {  try{
                if(order=="quit") break;
                if(order=="push") {
                    int num;
                    cin>>num;
                    v.push_back(num);
                }else if(order=="put")
                {
                    int a,b;
                    cin>>a>>b;
                    v[a]=b;
                }else
                {
                    int index;
                    cin>>index;
                    cout<<" "<<v.at(index);
                } 
				}
               catch(const int index)
        {
        cout<<" "<<"invalid index:"<<index;
        } 
            }

       
           
        }else
        {  int n;
            cin>>n;
            myvector<string>v(n);
            
            while(cin>>order)
            {try{
                if(order=="quit") break;
                if(order=="push") {
                    string str;
                    cin>>str;
                    v.push_back(str);
                }else if(order=="put")
                {
                    int a;string str;
                    cin>>a>>str;
                    v[a]=str;
                }else
                {
                    int index;
                    cin>>index;
                    cout<<" "<<v.at(index);
                }
            }
       
           catch(const int index)
    {
        cout<<" "<<"invalid index:"<<index;
    }
	} 
	}
         cout<<endl;
    }
}

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
using	namespace	std;

class	CException {
	string	m_errMsg;
public :
	CException (string	errMsg, int i) : m_errMsg (errMsg) {
		stringstream	stream;
		stream << i;
		string	str;
		stream >> str;
		m_errMsg += str;
	}
	string	what () {
		return m_errMsg;
	}
};
template <class	T>
class	CMyVector {
private :
	T	*m_pBuffer;     //存放元素缓冲区指针
	int	    m_size,m_bufferSize; //存放元素个数,缓冲区大小
public:

	CMyVector () : m_size (0), m_bufferSize (0),m_pBuffer (NULL) {}
	CMyVector (int n) : m_size (n), m_bufferSize (n) {
		if  (m_bufferSize > 0)
			m_pBuffer = new T [m_bufferSize];
		else
			m_pBuffer = NULL;
	}

	~CMyVector () {
		delete [] m_pBuffer;
	}
    CMyVector (const CMyVector &rhs);//复制构造
//    CMyVector (CMyVector &&rhs);  //C++11 移动构造
    CMyVector& operator = (const CMyVector &rhs);//复制赋值
//    CMyVector& operator = (CMyVector &&rhs);//C++ 11 移动赋值

	void	push_back (const T &rhs);  //添加元素

	T  & operator [] (int i) {   //下标运算符重载
		if (i < 0 || i >= m_size) {
			string	errMsg ("invalid index:");

			throw	CException (errMsg, i);
		}
		return m_pBuffer [i];
	}
	int	size () const {//获得元素个数
		return m_size;
	}
};

template <class T>
void	CMyVector<T>::push_back (const T &rhs)
{
	if  (m_bufferSize <= m_size) {//空间不够,作必要扩充
		T	*p = new T [m_bufferSize  + 10]; //避免经常扩充
		for (int i= 0; i < m_size; i++)
			p [i] = m_pBuffer [i];

		delete [] m_pBuffer;
		m_pBuffer = p;
		m_bufferSize += 10;
	}
	m_pBuffer [m_size++] = rhs;
}
template <class T>
CMyVector<T>::CMyVector (const CMyVector<T> &rhs)
: m_pBuffer(NULL),m_size (rhs.m_size), m_bufferSize (rhs.m_bufferSize)
{
		m_pBuffer = new T [m_bufferSize];
		for (int i= 0; i < m_size; i++)
			m_pBuffer [i] = rhs.m_pBuffer [i];
}
/*
template <class T>
CMyVector<T>::CMyVector(CMyVector<T> &&rhs)
:m_size (rhs.m_size), m_bufferSize (rhs.m_bufferSize),m_pBuffer (rhs.m_pBuffer)
{
    rhs.m_pBuffer = NULL;
    rhs.m_size = 0;
    rhs.m_buffersize = 0;
}
*/
template <class T>
CMyVector<T>& CMyVector<T>::operator = (const CMyVector<T> &rhs)
{
    this->~CMyVector();
    m_size = rhs.m_size, m_bufferSize = rhs.m_bufferSize,m_pBuffer = NULL;
	m_pBuffer = new T [m_bufferSize];
	for (int i= 0; i < m_size; i++)
		m_pBuffer [i] = rhs.m_pBuffer [i];
    return *this;
}
/*
template <class T>
CMyVector<T>& CMyVector<T>::operator = (CMyVector<T> &&rhs)
{
    swap (m_pBuffer, rhs.m_pBuffer);
    swap (m_bufferSize,rhs.m_bufferSize);
    swap (m_size, rhs.m_size);
}
*/
template	<class T>
void Parse (int size)
{
	CMyVector<T>	V (size);

	string	cmd;
	T		x;
	int		index;
	while (cin >> cmd) {
		try {
			if (cmd == "quit")
				break;
			else if (cmd == "push") {
				cin >> x;
				V.push_back (x);
			} else if (cmd == "fetch") {
				cin >> index;
				x = V [index];
				cout << ' '<< x;

			} else if (cmd == "put") {
				cin >> index >> x;
				V [index] = x;
			}
		} catch (CException &ex) {
			cout << ' ' << ex.what ();
		}
	}
	cout << endl;
}

int	main ()
{
	string	typeStr;
	while (cin >> typeStr) {
		int	size;
		cin >> size;
		if (typeStr == "int")
			Parse<int> (size);
		else
			Parse<string> (size);
	}

	return 0;
}



stringstream
功能大概是不同类型的 转换

#include<bits/stdc++.h>
using namespace std;
int main()
{
    stringstream  is;
    int k;
    cin>>k;
    string m;
    is<<k;
    is>>m;
    cout<<m;
}

  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值