c++ STL stack容器

 先进后出

#include
#include
using namespace std;
//基本数据类型
void test()
{
  stack s;
  //入栈
  for(int i = 0; i < 10; i++)
  {
    s.push(i+1);
  }
  cout<<"栈的大小:"< s1;
  s1.push(t1);
  s1.push(t2);
  s1.push(t3);
  while (!s1.empty())
  {
    teacher temp = s1.top();  //获取栈顶元素
    temp.printT();
    s1.pop();
  }
}

//自定义数据类型指针
void test2()
{
  teacher t1,t2,t3;
  t1.age = 10;
  t2.age = 20;
  t3.age = 30;
  stack s2;
  s2.push(&t1);
  s2.push(&t2);
  s2.push(&t3);
  while (!s2.empty())
  {
    teacher* temp = s2.top();  //获取栈顶元素
    temp->printT();   //等价 (*temp).printT();
    s2.pop();
  }
}

int main()
{
  test();
  test1();
  test2();
  return 0;
}

你可能感兴趣的:(C++,c++,java,算法)