第九层(5):STL之stack

简介: 第九层(5):STL之stack

前情回顾


上一块石碑,我学到了deque容器内函数的基本使用,同时下一块石碑也显露出来…


🚄上章地址:第九章(4):STL之deque


stack


概念


stack容器是栈容器,同时它也拥有栈一样的性质,先进后出。


0a2653c851af460fa595bd959398a8f1.png


stack容器需要注意的地方


stack容器是先进后出的数据结构,它能被访问到的只有栈顶,因此栈不可以被遍历,遍历属于非质变算法,因为栈只能访问到第一个元素,当遍历的时候,访问到第一个,访问第二个的时候,就得先把第一个取出来,让第二个变成栈顶,这个时候就属于是质变算法,就不属于遍历的范围了,但是栈是可以用size函数来返回栈内有多少个元素个数的,那是为什么呢?不能取出,不能遍历,用什么方法来统计内部元素?其实可以在进栈的时候统计,进去一个加一,出栈出去一个减一。


stack类内的构造函数


stack是只有默认构造和拷贝构造的

stack< T >;//默认构造
stack(const stack& s);//拷贝构造函数,可以将s的数据拷贝到本身


使用:


#include<stack>
#include<iostream>
using namespace std;
void test1()
{
  stack<int> s;
  s.push(1);
  stack<int> s1(s);
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png


stack类内的赋值操作


stack中的赋值操作只有一种

stack& operator=(const stack &s);//操作符重载,将s的数据拷贝到本身


使用:


#include<stack>
#include<iostream>
using namespace std;
void test1()
{
  stack<int> s;
  s.push(2);
  cout << s.top() << endl;
  stack<int> s1;
  s1 = s;
  cout << s1.top()<<endl;
}
int main()
{
  test1();
  return 0;
}

0eacb84100b54626af849e6b562bf92a.png


stack类内的插入


因为stack容器只能访问到栈顶,插入数据也只能从栈顶插入,所以stack就只有一种插入方式

push(T elem);//向栈顶插入元素


使用:


#include<stack>
#include<iostream>
using namespace std;
void test1()
{
  stack<int> s;
  s.push(2);
  cout << s.top() << endl;
}
int main()
{
  test1();
  return 0;
}

2d65d23f6d4748949b924e4057485923.png


stack类内的删除


同插入,只能访问到栈顶,所以只有一种删除方式

pop();//从栈顶删除第一个元素


使用:


#include<stack>
#include<iostream>
using namespace std;
void test1()
{
  int i = 2;
  stack<int> s;
  while (i--)
  {
  s.push(i);
  }
  cout << s.top() << endl;
  s.pop();
  cout << s.top() << endl;
}
int main()
{
  test1();
  return 0;
}

2e9b90b2ca334476abebe75bafe6eeaa.png


stack类内的访问


和插入删除一样,只能访问到栈顶元素,使用只有一种访问方式

top();//访问栈顶元素


使用:


#include<stack>
#include<iostream>
using namespace std;
void test1()
{
  stack<int> s;
  s.push(1);
  cout << s.top() << endl;
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png


stack类内的大小操作


在注意中提到,stack可以用size返回类内元素的多少,那除了size还有其他的函数可以进行操作吗?有,empty可以用来判断栈是否为空

empty();//用于判断栈是否为空,如果是空返回真
size();//返回栈内元素个数


使用:


#include<stack>
#include<iostream>
using namespace std;
void test1()
{
  stack<int> s;
  if (s.empty())
  {
  cout << "s为空" << endl;
  }
  cout << s.size() << endl;
}
int main()
{
  test1();
  return 0;
}

0eacb84100b54626af849e6b562bf92a.png


下一座石碑


这座石碑倒下了,露出了下一座石碑…


😘预知后事如何,关注新专栏,和我一起征服C++这座巨塔

🚀专栏:C++爬塔日记

🙉都看到这里了,留下你们的👍点赞+⭐收藏+📋评论吧🙉


相关文章
|
4月前
|
容器
STL_stack
STL_stack
17 1
|
29天前
|
C++ 容器
【C++初阶】STL详解(六)Stack与Queue的介绍与使用
【C++初阶】STL详解(六)Stack与Queue的介绍与使用
19 1
|
29天前
|
存储 C++ 容器
【C++初阶】STL详解(七)Stack与Queue的模拟实现
【C++初阶】STL详解(七)Stack与Queue的模拟实现
13 1
|
6月前
|
容器
STL-stack
STL-stack
30 0
|
11月前
|
设计模式 C++ 容器
C++【STL】之stack和queue学习
C++ STL stack和queue常用接口和模拟实现详细讲解,干货满满!
83 0
C++【STL】之stack和queue学习
|
算法 C++ 容器
第九层(6):STL之queue
第九层(6):STL之queue
第九层(6):STL之queue
第九层(3):STL之vector类(下)
第九层(3):STL之vector类(下)
第九层(3):STL之vector类(下)
第九层(3):STL之vector类(上)
第九层(3):STL之vector类(上)
第九层(3):STL之vector类(上)
|
算法 C++ 容器
第九层(7):STL之list(下)
第九层(7):STL之list(下)
第九层(7):STL之list(下)
|
存储 C++ 容器
第九层(7):STL之list(上)
第九层(7):STL之list(上)
第九层(7):STL之list(上)