stack<int> st// 压入st.push(1)st.push(2)st.push(3)// 查看栈顶print st.top() // 3// 弹出st.pop() // 移除 3print st.top() // 2// 括号匹配stack<char> bracketfor each c in expression: if c is '(' or '[' or '{': bracket.push(c) else if c is ')' or ']' or '}': if bracket.empty(): return false top = bracket.top() if not match(top, c): return false bracket.pop()return bracket.empty()// 十进制转二进制stack<int> bitswhile n > 0: bits.push(n % 2) n = n / 2while not bits.empty(): print bits.top() bits.pop()```cppstack 没有 `clear()`,清空用 `while (!empty()) pop()` 或 `st = stack<int>()`。stack 没有迭代器,遍历只能通过反复 top() + pop() 清空式输出。## 相关链接- [[../../../数据结构/G_栈_Stack]]- [[../../../数据结构/G_栈_Stack]]- [[queue]]