push
语法:
1 |
void push(const value_type& __x); |
push()函数在栈顶增加元素
1 2 3 4 5 6 7 8 9 10 11 12 13 |
源代码 /** * @brief Add data to the top of the %stack. * @param x Data to be added. * * This is a typical %stack operation. The function creates an * element at the top of the %stack and assigns the given data * to it. The time complexity of the operation depends on the * underlying sequence. */ void push(const value_type& __x) { c.push_back(__x); } |
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// 梁笔记 // https://zouzhongliang.com #include <iostream> #include <stack> using namespace std; int main() { stack<int> Si; Si.push(1); //栈顶增加元素,压栈 Si.push(2); Si.push(3); Si.push(4); Si.push(4); Si.push(5); while(!Si.empty()){ cout<<Si.top()<<endl; Si.pop(); } } |
测试结果
1 2 3 4 5 6 |
5 4 4 3 2 1 |