Skip to content

cpp_std_stack

jiaxw32 edited this page Jul 18, 2020 · 4 revisions

stack 类

模版定义

template <class Type, class Container= deque <Type>>
class stack
  • Type: 要存储在堆栈中的元素数据类型。
  • 用来实现堆栈的基础容器的类型。 默认值为 deque <Type> 类。

适合堆栈的基础容器类包括deque、 list 类和vector 类, 或者支持back、 push_back和pop_back操作的任何其他序列容器。

成员函数

构造函数

构造一个空的堆栈或者是基容器类副本的堆栈。

stack();
explicit stack(const container_type& right);

示例:

#include <stack>
#include <vector>
#include <list>
#include <iostream>

int main( )
{
   using namespace std;

   // Declares stack with default deque base container
   stack <char> dsc1;

   //Explicitly declares a stack with deque base container
   stack <char, deque<char> > dsc2;

   // Declares a stack with vector base containers
   stack <int, vector<int> > vsi1;

   // Declares a stack with list base container
   stack <int, list<int> > lsi;

   // The second member function copies elements from a container
   vector<int> v1;
   v1.push_back( 1 );
   stack <int, vector<int> > vsi2( v1 );
   cout << "The element at the top of stack vsi2 is "
        << vsi2.top( ) << "." << endl; //output: 1
}

push

向堆栈顶部添加一个元素。堆栈顶部是最近添加的元素所占据的位置,并且是容器末尾处的最后一个元素。

void push(const Type& val);

示例

#include <stack>
#include <iostream>

int main( )
{
   using namespace std;
   stack <int> s1;

   s1.push( 10 );
   s1.push( 20 );
   s1.push( 30 );

   stack <int>::size_type i;
   i = s1.size( );
   cout << "The stack length is " << i << "." << endl;

   i = s1.top( );
   cout << "The element at the top of the stack is "
        << i << "." << endl;
}

top

返回对堆栈顶部元素的引用。

reference top();
const_reference top() const;

如果将的top返回值分配const_reference给, 则无法修改堆栈对象。 如果将的top返回值分配reference给, 则可以修改堆栈对象。

示例

#include <stack>
#include <iostream>

int main( )
{
   using namespace std;
   stack <int> s1;

   s1.push( 1 );
   s1.push( 2 );

   int& i = s1.top( );
   const int& ii = s1.top( );

   cout << "The top integer of the stack s1 is "
        << i << "." << endl; //output: 2
   i--;
   cout << "The next integer down is "<< ii << "." << endl;  //output: 1
}

pop

从堆栈的顶部删除元素

void pop();

示例

#include <stack>
#include <iostream>

int main( )
{
   using namespace std;
   stack <int> s1;

   s1.push( 10 );
   s1.push( 20 );
   s1.push( 30 );

   stack <int>::size_type i;
   i = s1.size( );
   cout << "The stack length is " << i << "." << endl;

   i = s1.top( );
   cout << "The element at the top of the stack is "
        << i << "." << endl;

   s1.pop( );

   i = s1.size( );
   cout << "After a pop, the stack length is "
        << i << "." << endl;

   i = s1.top( );
   cout << "After a pop, the element at the top of the stack is "
        << i << "." << endl;
}

输出

The stack length is 3.
The element at the top of the stack is 30.
After a pop, the stack length is 2.
After a pop, the element at the top of the stack is 20.

empty

测试堆栈是否为空,如果堆栈为空,则为 true;如果堆栈不为空,则为 false。

bool empty() const;

示例:

#include <stack>
#include <iostream>

int main( )
{
   using namespace std;
   // Declares stacks with default deque base container
   stack <int> s1, s2;

   s1.push( 1 );

   if ( s1.empty( ) )
      cout << "The stack s1 is empty." << endl;
   else
      cout << "The stack s1 is not empty." << endl;

   if ( s2.empty( ) )
      cout << "The stack s2 is empty." << endl;
   else
      cout << "The stack s2 is not empty." << endl;
}

size

返回堆栈中元素数。

size_type size() const;

示例

#include <stack>
#include <iostream>

int main( )
{
   using namespace std;
   stack <int> s1;
   stack <int>::size_type i;

   s1.push( 1 );
   i = s1.size( );
   cout << "The stack length is " << i << "." << endl;

   s1.push( 2 );
   i = s1.size( );
   cout << "The stack length is now " << i << "." << endl;
}

参考资料

Clone this wiki locally