- //
- // CopyRight(c) 2009, YOYO, All Rights Reserved.
- // Author: LIN YiQian
- // Created: 2009/08/24
- // Describe: STL stack 使用DEMO
- //
- #include <iostream>
- #include <stack>
- using namespace std;
- typedef stack<int> INT_STK;
- void main(void)
- {
- INT_STK stkInt;
- // Print is stack empty?
- cout << "Stack Empty?: " << boolalpha << stkInt.empty() << endl;
- // Push elements
- for (int i = 0; i < 10; i += 2)
- {
- stkInt.push(i);
- }
- // Get size
- cout << "Stack size: " << stkInt.size() << endl;
- // Get top element & change its value
- if (!stkInt.empty())
- {
- cout << "Top Element: " << stkInt.top() << " change to 99 " << endl;
- stkInt.top() = 99;
- }
- // Print stack
- {
- while (!stkInt.empty())
- {
- cout << stkInt.top() << " ";
- stkInt.pop();
- }
- }
- cout << endl;
- system("pause");
- }