C++ STL栈容器以及 .front()函数

10 篇文章 2 订阅

c++ stl栈stack介绍

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

c++ stl栈stack的头文件为

#include <stack> 

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

操作 比较和分配堆栈

empty() 堆栈为空则返回真

pop() 移除栈顶元素

push() 在栈顶增加元素

size() 返回栈中元素数目

top() 返回栈顶元素

c++ stl栈stack用法代码举例1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "stdafx.h"  
#include <stack>  
#include <vector>  
#include <deque>  
#include <iostream>  
   
using  namespace  std;  
   
int  _tmain( int  argc, _TCHAR* argv[])  
{  
     deque< int > mydeque(2,100);  
     vector< int > myvector(2,200);  
   
     stack< int > first;  
     stack< int > second(mydeque);  
   
     stack< int ,vector< int > > third;  
     stack< int ,vector< int > > fourth(myvector);  
   
     cout <<  "size of first: "  << ( int ) first.size() << endl;  
     cout <<  "size of second: "  << ( int ) second.size() << endl;  
     cout <<  "size of third: "  << ( int ) third.size() << endl;  
     cout <<  "size of fourth: "  << ( int ) fourth.size() << endl;  
   
   
     return  0;  
}

c++ stl栈stack用法代码举例2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// stack::empty  
#include <iostream>  
#include <stack>  
using  namespace  std;  
   
int  main ()  
{  
   stack< int > mystack;  
   int  sum (0);  
   
   for  ( int  i=1;i<=10;i++) mystack.push(i);  
   
   while  (!mystack.empty())  
   {  
      sum += mystack.top();  
      mystack.pop();  
   }  
   
   cout <<  "total: "  << sum << endl;  
     
   return  0;  
}

 c++ stl栈stack用法代码举例3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// stack::push/pop  
#include <iostream>  
#include <stack>  
using  namespace  std;  
   
int  main ()  
{  
   stack< int > mystack;  
   
   for  ( int  i=0; i<5; ++i) mystack.push(i);  
   
   cout <<  "Popping out elements..." ;  
   while  (!mystack.empty())  
   {  
      cout <<  " "  << mystack.top();  
      mystack.pop();  
   }  
   cout << endl;  
   
   return  0;  
}

 c++ stl栈stack用法代码举例4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>  
#include <stack>  
using  namespace  std;  
   
int  main ()  
{  
   stack< int > mystack;  
   
   for  ( int  i=0; i<5; ++i) mystack.push(i);  
   
   cout <<  "Popping out elements..." ;  
   while  (!mystack.empty())  
   {  
      cout <<  " "  << mystack.top();  
      mystack.pop();  
   }  
   cout << endl;  
   
   return  0;  
}

.front()函数

① front()返回的是reference类型。 这和C++里面的变量引用,的确是一个意思; 其目的就是为了方便容器中元素的访问(读、写);对于只读的,可以是const_reference;

②  其实提供的reference不止front和back,比如at,可以是任何容器中任何一个元素;还有operator[]提供随机访问;

③ 具体应用,就是为了方便的读写;vector,deque,list还有string都有front和back,以vector为例, 应用如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>
 
int  main ()
{
   std::vector< int > v;
 
   v.push_back(10);
   v.push_back(2);
 
   v.front() -= v.back();
 
   std::cout <<  " front() is now. "  << v.front() <<  '\n' ;
 
   return  0;
}

结果:

1
front() is now 8.

  • 11
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值