
基础
stack是一种先进后出(First In Last Out,FILO)的数据结构,它只有一个出口。
栈中只有顶端的元素才可以被外界使用,因此栈不允许有遍历行为。
栈中进入数据称为 —- 入栈 push
栈中弹出数据称为 —- 出栈 pop

代码
Talk is cheap, show me the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #include<iostream> using namespace std; #include<stack>
void test01() { stack<int> s1; for (int i = 0; i < 10; i++) { s1.push(i); }
stack<int> s2(s1);
stack<int> s3; s3 = s2;
cout << "现在的栈大小:" << s3.size() << endl; while (!s3.empty()) { cout << "栈顶的元素是:" << s3.top() << endl; s3.pop(); cout << "现在的栈大小:" << s3.size() << endl; }
}
int main() { test01(); system("pause"); return 0; }
|
Nothing Gonna Change My Love For You
Westlife