#include<String>
struct Person
{
std::string name;
int age;
Person(std::string n, int a) :name(n), age(a)
{
}
Person()
{
}
};
namespace VGP130
{
template<typename T, size_t n>
class Stack
{
public:
explicit Stack() :top(-1), size(n)
{
}
~Stack() = default;
bool push(const T& a)
{
if (!isFull())
{
data[++top] = a;
return true;
}
return false;
}
bool isFull() const
{
return top == size - 1;
}
bool isEmpty()const
{
return top == -1;
}
bool pop(T& ret)
{
if (isEmpty())
return false;
ret = data[top--];
return true;
}
void print()
{
std::cout << "Stack: ";
for (int i = 0; i <= top; ++i)
std::cout << data[i] << std::endl;
std::cout << std::endl;
}
private:
T data[n];
int top;
size_t size;
};
}
#include<iostream>
#include "Person.h"
#include "Stack.h"
using namespace std;
using namespace VGP130;
int main()
{
Stack<Person, 5> personStack;
for (int i = 0; i < 5; ++i)
{
personStack.push(Person("what",2));
}
Person pValue{};
for (int i = 0; i < 5; ++i)
{
if (personStack.pop(pValue))
cout << "Top of peraonStack: " << pValue.name <<" "<<pValue.age <<endl;
else
cout << "pop from empty stack" << endl;
}
system("pause");
}
stack 是模板类,在main函数里实例化的时候,模板类型T是另一个结构体
Person
Stack<Person, 5> personStack
请问下面的代码是如何实现的:
for (int i = 0; i < 5; ++i)
{
personStack.push(Person("what",2));
}
数据是怎么导入的。