如何在C++中對堆棧進行操作

 //#include <iostream.h>
#include <stack.h>
void main()
/* Pre: The user supplies an integer n and n decimal numbers.
   Post:The numbers are printed in reverse order.
   Uses:The STL class stack and its methods */
{
 int n;
 double item;
   stack<double>numbers;//declares and intializes a stack of numbers.
   cout<<"Type in an integer n followed by n decimal numbers."<<endl;
   cout<<"The numbers will be printed in reverse order."<<endl;
   cin>>n;
   for(int i=0;i<n;i++)
   {
    cin>>item;
    numbers.push(item);
   }
   cout<<endl<<endl;
   while(!numbers.empty())
   {
    cout<<numbers.top()<<"  ";
    numbers.pop();
   }
   cout<<endl;
}
上面是實現是利用模版庫中的Stack<類型>變量名  來實現的,其中在他的類庫中還有很多這樣的操作,其中還有對

列表的操作,對各個類庫中的文件操作必須加上響應的頭文件例如本程序中就必須存在#include <stack.h>或者還

可以使用#include <stack>,第一種是以前老版本中使用的,後一種纔是現在的版本中所使用的,就象#include <iostream.h>一樣,是在C語言下使用的,而#include <iostream> using namespace std;纔是真正的C++中所使用

但是,他們的功能都是基本相同的,沒有什麼區別....

發佈了23 篇原創文章 · 獲贊 4 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章