用C++的流對象(Strstream)來搭建一個隊列

 

需要包含頭文件strstream

隊列模型

class Quene
{
private:
	char List[100];//隊列長度
	strstream queueObj;//流對象
public:
	Quene()
	{
		queueObj = strstream(List, 100, ios::in || ios::out);
	}
	void Insert(char *Buf)
	{
		queueObj << Buf << endl;
	}
	char* GetEle()
	{
		char *Buf = new char;
		queueObj >> Buf;
		return Buf;
	}
};

測試代碼

cout << "-------組1-----" << endl;
	Quene List1;
	char* T = new char;
	for (int i = 0; i < 3; i++)
	{
		cin >> T;
		List1.Insert(T);
	}
	cout << "---------------" << endl;
	for (int i = 0; i < 3; i++)
	{
		cout << List1.GetEle() << endl;
	}
	cout << "-----組2------" << endl;
	Quene List2;
	for (int i = 0; i < 3; i++)
	{
		cin >> T;
		List2.Insert(T);
	}
	cout << "---------------" << endl;
	for (int i = 0; i < 3; i++)
	{
		cout << List2.GetEle() << endl;
	}

測試效果

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

這兩天看譚浩強的書偶爾看到了一道strStream的例題,就想這東西並不使用本地文件能不能用來試試動態隊列,但是寫了一下比正兒八經用指針搭的隊列來說還是差挺多的。表現在數據成員過於單一,隊長度不好控制(本來想用sizeof做的但是實際效果不太好),真正用還是老老實實用指針搭吧,不過這東西可能最大的好處就是搭建簡單

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章