gen_stck.cpp

  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1194442938015&lmt=1194190197&format=336x280_as&output=html&correlator=1194442937843&url=file%3A%2F%2F%2FC%3A%2FDocuments%2520and%2520Settings%2Flhh1%2F%E6%A1%8C%E9%9D%A2%2FCLanguage.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=583001034.1194442938&ga_sid=1194442938&ga_hid=1942779085&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency"> #include <iostream.h>

const int SIZE = 100;

template <class SType> class stack {
   SType stck[SIZE];
   int tos;
 public:
   stack(void);
   ~stack(void);
   void push(SType i);
   SType pop(void);
 };

template <class SType> stack<SType>::stack()
 {
   tos = 0;
   cout << "Stack Initialized." << endl;
 }

template <class SType> stack<SType>::~stack()
 {
   cout << "Stack Destroyed." << endl;
 }

template <class SType> void stack<SType>::push(SType i)
 {
   if(tos==SIZE)
    {
      cout << "Stack is full." << endl;
      return;
    }
   stck[tos++] = i;
 }
template <class SType> SType stack<SType>::pop(void)
 {
   if(tos==0)
    {
      cout << "Stack underflow." << endl;
      return 0;
    }
   return stck[--tos];
 }

void main(void)
 {
   stack<int> a;
   stack<double> b;
   stack<char> c;
   int i;

   a.push(1);
   a.push(2);
   b.push(99.3);
   b.push(-12.23);

   cout << a.pop() << " ";
   cout << a.pop() << " ";
   cout << b.pop() << " ";
   cout << b.pop() << endl;

   for(i=0; i<10; i++)
      c.push((char) 'A' + i);
   for(i=0; i<10; i++)
      cout << c.pop();
   cout << endl;
 }

### C++ 中 `std::stack` 的基本用法 在 C++ 标准库中,`std::stack` 是一种容器适配器,提供了栈的数据结构特性。栈是一种后进先出 (LIFO, Last In First Out) 的数据结构。 #### 创建和初始化 Stack 可以使用默认构造函数来创建一个空的栈: ```cpp #include <iostream> #include <stack> int main() { std::stack<int> s; // 定义一个存储 int 类型元素的栈 return 0; } ``` 也可以通过指定底层容器的方式创建栈,默认情况下 `std::stack` 使用的是 `std::deque` 作为其内部实现[^1]。 #### 基本操作方法 - **压入元素**: 使用成员函数 `push()` 向栈顶添加新元素。 - **弹出元素**: 调用 `pop()` 函数移除栈顶元素,注意此操作不会返回被删除的值。 - **访问顶部元素**: 利用 `top()` 方法获取当前位于栈顶端的那个元素而不需要将其取出。 - **判断是否为空**: 可以调用 `empty()` 来检测栈内是否有任何元素存在。 - **获得大小**: 如果想要知道栈中有多少项,则可利用 `size()` 获取总数目。 下面给出一段完整的例子展示这些功能的应用: ```cpp #include <iostream> #include <stack> void showStack(const std::stack<int>& stck){ if(stck.empty()){ std::cout << "The stack is empty." << '\n'; return; } std::stack<int> temp_stack = stck; while (!temp_stack.empty()) { std::cout << ' ' << temp_stack.top(); temp_stack.pop(); } std::cout << '\n'; } int main(){ std::stack<int> mystack; for(int i=0;i<5;++i) mystack.push(i); std::cout<<"Initial stack content:"; showStack(mystack); // Accessing top element without removing it. std::cout<<"\nThe top of the stack contains:"<<mystack.top()<<'\n'; // Removing all elements from the stack one by one until it becomes empty. while(!mystack.empty()){ std::cout<< "\nPopping "<< mystack.top()<<" off the stack.\n"; mystack.pop(); if(!mystack.empty()) std::cout<<"Current stack after pop operation:\n"; else std::cout<<"Now the stack has been emptied.\n"; showStack(mystack); } return 0; } ``` 这段程序展示了如何向栈中加入五个整数,并逐步将它们全部移除并打印出来,在每次执行完一次 `pop` 操作之后都会显示剩余的内容给用户查看。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值