C++ Templates:Stack类编写(带有模板缺省值)

Stack类的普通实现

main函数 

#include "Stack.hpp"
#include <iostream>
using namespace std;
#include <string>

class Person
{
private:
	int age;
	string name;
public:
	Person() = default;
	Person(int age, string name)
	{
		this->age = age;
		this->name = name;
	}
	Person(Person& obj)
	{
		this->age = obj.age;
		this->name = obj.name;
	}
	Person& operator = (Person& obj)
	{
		this->age = obj.age;
		this->name = obj.name;
		return *this;
	}
	friend ostream& operator <<(ostream& cout, Person& obj);
	~Person() = default;
};

ostream& operator <<(ostream& cout, Person& obj)
{
	cout << obj.name << "的年龄为" << obj.age;
	return cout;
}

int main()
{
	Stack<Person> obj;
	obj.Push(Person(19, "张三"));
	obj.ShowInf();
}

Stack.hpp

#include <iostream>
using namespace std;

template <typename T = int>
class Stack
{
private:
	T* Buffer;
	int number_of_element;
public:
	Stack() {
		this->Buffer = nullptr;
		this->number_of_element = 0;
	}
	Stack(Stack& obj)
	{
		Buffer = new T[obj.number_of_element];
		this->number_of_element = obj.number_of_element;
		for (int i = 0; i < obj.number_of_element; i++)
		{
			Buffer[i] = obj.Buffer[i];
		}
	}
	void Push(T& obj)
	{
		T* New_Buffer = new T[this->number_of_element + 1];
		for (int i = 0; i < this->number_of_element; i++)
		{
			New_Buffer[i] = this->Buffer[i];
		}
		New_Buffer[this->number_of_element] = obj;

		if (this->Buffer != nullptr)
		{
			delete[] this->Buffer;
		}
		this->Buffer = New_Buffer;
		this->number_of_element++;
	}
	void Push(T&& obj)
	{
		T* New_Buffer = new T[this->number_of_element + 1];
		for (int i = 0; i < this->number_of_element; i++)
		{
			New_Buffer[i] = this->Buffer[i];
		}
		New_Buffer[this->number_of_element] = obj;

		if (this->Buffer != nullptr)
		{
			delete[] this->Buffer;
		}
		this->Buffer = New_Buffer;
		this->number_of_element++;
	}
	void Pop()
	{
		T* New_Buffer = new T[this->number_of_element - 1];
		for (int i = 0; i < this->number_of_element - 1; i++)
		{
			New_Buffer[i] = this->Buffer[i];
		}
		if (this->Buffer != nullptr)
		{
			delete[] this->Buffer;
		}
		this->Buffer = New_Buffer;
		this->number_of_element--;
	}
	void ShowInf()
	{
		for (int i = 0; i < this->number_of_element; i++)
		{
			cout << this->Buffer[i] << " ";
		}
		cout << endl;
	}
	~Stack()
	{
		if (this->Buffer != NULL)
		{
			delete[] this->Buffer;
			this->number_of_element = 0;
		}
	}
};

带有Stack模板缺省值的Stack类

Stack.hpp

#include <iostream>
using namespace std;
#include <vector>

// 模板的缺省值
template <class T, class CONT = vector<T> >
class Stack
{
private:
	CONT element;
	int Size;
public:
	Stack();
	Stack(T obj);
	Stack(vector<T> obj);

	void Push(T obj);
	void Pop();
	T& Top();
	bool Empty();
	int PrintSize();
	T& At(int order);
};

template <class T, class CONT /*= vector<T> */>
T& Stack<T, CONT>::At(int order)
{
	if (order >= this->Size)
	{
		throw out_of_range("over array range!");
	}
	return this->element.at(order);
}

template <class T, class CONT /*= vector<T> */>
int Stack<T, CONT>::PrintSize()
{
	return this->Size;
}

template <class T, class CONT /*= vector<T> */>
bool Stack<T, CONT>::Empty()
{
	return this->element.empty();
}

template <class T, class CONT /*= vector<T> */>
T& Stack<T, CONT>::Top()
{
	if (this->Size == 0)
	{
		throw out_of_range("Stack is empty!");
	}
	return *(this->element.end());
}

template <class T, class CONT /*= vector<T> */>
void Stack<T, CONT>::Pop()
{
	if (this->Size == 0)
	{
		throw out_of_range("Stack is empty!");
	}
	this->element.pop_back();
	this->Size--;
}

template <class T, class CONT /*= vector<T> */>
void Stack<T, CONT>::Push(T obj)
{
	this->element.push_back(obj);
	this->Size++;
}

template <class T, class CONT /*= vector<T> */>
Stack<T, CONT>::Stack(vector<T> obj)
{
	this->Size = 0;
	this->element.clear();

	this->element = obj;
	this->Size = obj.size();
}

template <class T, class CONT /*= vector<T> */>
Stack<T, CONT>::Stack(T obj)
{
	this->Size = 0;
	this->element.clear();

	this->element.push_back(obj);
	this->Size++;
}

template <class T, class CONT /*= vector<T> */>
Stack<T, CONT>::Stack()
{
	this->Size = 0; // 赋值前一定要初始化
	this->element.clear();
}

main.cpp

#include <iostream>
#include <sstream>
using namespace std;
#include <string>
#include <deque>
#include "Stack.hpp"

class Person
{
private:
	string name;
	int age;
	char* CharOutput;
public:
	Person(string name, int age) {
		this->name = name;
		this->age = age;
		CharOutput = NULL;
	}
	Person(const Person& obj) {
		this->age = obj.age;
		this->name = obj.name;
		CharOutput = NULL;
	}
	Person(const Person&& obj) {
		this->age = obj.age;
		this->name = obj.name;
		CharOutput = NULL;
	}
	operator char* ();
	~Person();
};

Person::operator char*()
{
	ostringstream ostr;
	ostr << this->name << "的年龄为" << this->age;
	this->CharOutput = new char[ostr.str().size() + 1];
	strcpy(this->CharOutput, ostr.str().c_str());
	return this->CharOutput;
}

Person::~Person()
{
	if (this->CharOutput != nullptr)
	{
		delete[] this->CharOutput;
	}
}

int main()
{
	Stack<Person> Stack_Obj1(Person("张三", 19));
	try
	{
		cout << Stack_Obj1.At(0) << endl;
	}
	catch (const out_of_range& exp)
	{
		cout << exp.what() << endl;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_45590473/article/details/112118632
今日推荐