top
语法:
1 |
reference top(); |
top()返回栈顶元素 。
1 2 3 4 5 6 7 8 9 10 11 |
源代码 /** * Returns a read/write reference to the data at the first * element of the %stack. */ reference top() { __glibcxx_requires_nonempty(); return c.back(); } |
测试代码
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 |