tsaiwn@ccbsd5 % cat -n stk4.cpp 1 //stk4.cpp, CopyLeft by tsaiwn@csie.nctu.edu.tw 2 // g++ stk4.cpp -Wno-deprecated ; ./a.out 3 // 其實, C++ Class library 已經有 stack, 4 //是以 template (樣板) 方式定義; 使用上更為方便! 5 //但其函數名稱與用法與一般資料結構課本上講的稍有不同; 6 //例如, 一般課本 pop不是void function. 7 // 又如是否空堆疊用 empty( ) 不是 isempty( ) 8 // 因為C++ 的一些資料結構class來自HP公司的 STL (Standard Template Library), 9 //在更早時要 #include 才能使用 STL 裡面的 class. 10 //後來, STL在1999年正式被納入 C++ 類別程式庫中, 可#include 各自的 header即可 11 // 12 #include 13 #include 14 int main( ){ 15 stack x; // STL 中的 stack 是 template 16 x.push(880); 17 x.push(770); 18 x.push(53); 19 while(!x.empty()){ 20 cout << x.top(); x.pop(); 21 } 22 cout << endl; 23 return 0; 24 } tsaiwn@ccbsd5 % g++ stk4.cpp warning: #warning This file includes at least one deprecated or antiquated header. ... To disable this warning use -Wno-deprecated. tsaiwn@ccbsd5 % ./a.out 53770880 tsaiwn@ccbsd5 % cat -n stk5.cpp 1 //stk5.cpp, CopyLeft by tsaiwn@csie.nctu.edu.tw 2 // g++ stk5.cpp ; ./a.out 3 #include 4 #include 5 // 新寫法沒有 .h 但要記得如下列的 using namespace std; 6 using namespace std; // 因為C++ class library被集合到命名空間 std:: 7 int main( ) { 8 stack x; // STL 中的 stack 是 template 9 x.push(880); 10 x.push(770); 11 x.push(53); 12 while(!x.empty()){ 13 cout << x.top() << " "; x.pop(); 14 } 15 cout << endl; 16 return 0; 17 } tsaiwn@ccbsd5 % g++ stk5.cpp tsaiwn@ccbsd5 % g++ ./a.out 53 770 880 tsaiwn@ccbsd5 % exit exit tsaiwn@ccbsd5 % cat /usr/include/g++/stl.h // -*- C++ -*- compatibility header. // This file is part of the GNU ANSI C++ Library. #include #include #include #include #include #include #include #include #include #include #include #include tsaiwn@ccbsd5 %