码迷,mamicode.com
首页 > 其他好文 > 详细

stack功能

时间:2020-05-05 18:05:38      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:数据   返回   tab   top   highlight   class   swap   文件   www   

C++ Stack(堆栈) 是一个容器的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构。

c++ stl栈stack的头文件为

#include <stack> 

 

c++ stl栈stack的成员函数介绍

empty()  堆栈为空则返回真

pop()     移除栈顶元素

push()   在栈顶增加元素

size()    返回栈中元素数目

top()      返回栈顶元素

swap()  交换栈中元素

 

构造示例:

// constructing stacks
#include <iostream>       // std::cout
#include <stack>          // std::stack
#include <vector>         // std::vector
#include <deque>          // std::deque

int main ()
{
  std::deque<int> mydeque (3,100);          // deque with 3 elements
  std::vector<int> myvector (2,200);        // vector with 2 elements

  std::stack<int> first;                    // empty stack
  std::stack<int> second (mydeque);         // stack initialized to copy of deque

  std::stack<int,std::vector<int> > third;  // empty stack using vector
  std::stack<int,std::vector<int> > fourth (myvector);

  std::cout << "size of first: " << first.size() << \n;
  std::cout << "size of second: " << second.size() << \n;
  std::cout << "size of third: " << third.size() << \n;
  std::cout << "size of fourth: " << fourth.size() << \n;

  return 0;
}

Output:

size of first: 0
size of second: 3
size of third: 0
size of fourth: 2

 

push/pop/empty 示例:

// stack::push/pop
#include <iostream>       // std::cout
#include <stack>          // std::stack

int main ()
{
  std::stack<int> mystack;

  for (int i=0; i<5; ++i) mystack.push(i);

  std::cout << "Popping out elements...";
  while (!mystack.empty())
  {
     std::cout <<   << mystack.top();
     mystack.pop();
  }
  std::cout << \n;

  return 0;
}

Output:

Popping out elements... 4 3 2 1 0

 

size 示例:

// stack::size
#include <iostream>       // std::cout
#include <stack>          // std::stack

int main ()
{
  std::stack<int> myints;
  std::cout << "0. size: " << myints.size() << \n;

  for (int i=0; i<5; i++) myints.push(i);
  std::cout << "1. size: " << myints.size() << \n;

  myints.pop();
  std::cout << "2. size: " << myints.size() << \n;

  return 0;
}

Output:

0. size: 0
1. size: 5
2. size: 4

 

top 示例:

// stack::top
#include <iostream>       // std::cout
#include <stack>          // std::stack

int main ()
{
  std::stack<int> mystack;

  mystack.push(10);
  mystack.push(20);

  mystack.top() -= 5;

  std::cout << "mystack.top() is now " << mystack.top() << \n;

  return 0;
}

Output:

mystack.top() is now 15

 

swap 示例

// stack::swap
#include <iostream>       // std::cout
#include <stack>          // std::stack

int main ()
{
  std::stack<int> foo,bar;
  foo.push (10); foo.push(20); foo.push(30);
  bar.push (111); bar.push(222);

  foo.swap(bar);

  std::cout << "size of foo: " << foo.size() << \n;
  std::cout << "size of bar: " << bar.size() << \n;

  return 0;
}

Output:

size of foo: 2
size of bar: 3

 

 

stack功能

标签:数据   返回   tab   top   highlight   class   swap   文件   www   

原文地址:https://www.cnblogs.com/mingguzhou/p/12831231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!