在 C++ 中,标准库提供了多种容器和算法来帮助开发者更高效地编写程序。
<stack>
是 C++ 标准模板库(STL)的一部分,它实现了一个后进先出(LIFO,Last In First Out)的数据结构。这种数据结构非常适合于需要"最后添加的元素最先被移除"的场景。
<stack>
容器适配器提供了一个栈的接口,它基于其他容器(如 deque
或 vector
)来实现。栈的元素是线性排列的,但只允许在一端(栈顶)进行添加和移除操作。
// <stack> -*- C++ -*-
// Copyright (C) 2001-2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/*
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*
* Copyright (c) 1996,1997
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
/** @file include/stack
* This is a Standard C++ Library header.
*/
#ifndef _GLIBCXX_STACK
#define _GLIBCXX_STACK 1
#pragma GCC system_header
#include <deque>
#include <bits/stl_stack.h>
#endif /* _GLIBCXX_STACK */
基本操作
push()
: 在栈顶添加一个元素。pop()
: 移除栈顶元素。top()
: 返回栈顶元素的引用,但不移除它。empty()
: 检查栈是否为空。size()
: 返回栈中元素的数量。
语法
以下是使用 <stack>
的基本语法:
#include <iostream> #include <stack> int main() { std::stack<int> s; // 向栈中添加元素 s.push(1); s.push(2); s.push(3); // 访问栈顶元素 std::cout << "Top element is: " << s.top() << std::endl; // 移除栈顶元素 s.pop(); std::cout << "After popping, top element is: " << s.top() << std::endl; // 检查栈是否为空 if (!s.empty()) { std::cout << "Stack is not empty." << std::endl; } // 打印栈的大小 std::cout << "Size of stack: " << s.size() << std::endl; return 0; }