C++之STL-栈Stack
#include <iostream>
#include <stack>
using namespace std;
void new_stack(stack<int> stack_use) {
stack<int> int_stack_container = stack_use;
while (!int_stack_container.empty()) {
cout << "\t" << int_stack_container.top();
int_stack_container.pop();
}
cout << endl;
}
int main() {
stack<int> stack_1;
stack_1.push(1);
stack_1.push(2);
stack_1.push(3);
stack_1.push(5);
stack_1.push(6);
cout << "stack:";
new_stack(stack_1);
cout << "stack_size:" << stack_1.size() << endl;
cout << "stack_top:" << stack_1.top() << endl;
cout << "stack_pop:";
//先进后出,后进先出FILO
stack_1.pop();
new_stack(stack_1);
return 0;
}
输出:
stack: 6 5 3 2 1
stack_size:5
stack_top:6
stack_pop: 5 3 2 1
通过上述输出能够发现,栈的特性就是先进后出(FILO),后进先出(LIFO),按照压栈的顺序 1->2->3->5->6,那么处于栈顶的元素即为6,栈中自顶而下顺序为6->5->3->2->1。
top()
:输出栈顶元素
pop()
:删除栈尾元素,源码中是调用了pop_back(),对其进行了封装。