STL容器--stack

1特點

  • 先進後出
  • 不能進行遍歷
  • 不支持隨機存儲,只能從棧頂獲取和插入元素
  • 沒有迭代器

在這裏插入圖片描述

2常用API

2.1構造函數

在這裏插入圖片描述

2.2stack賦值操作函數

在這裏插入圖片描述

2.3stack數據存儲操作

在這裏插入圖片描述

2.4stack大小操作

在這裏插入圖片描述

3代碼使用

#include<iostream>
#include<stack>
using namespace std;
void tses1(){
	//初始化
	stack<int> s1;
	stack<int> s2(s1);
	
	//stack 操作
	//增加操作
	s1.push(10);
	s1.push(10);
	s1.push(10);
	s1.push(10);
	//刪除操作
	s1.pop();
	//打印容器數據
	while(!s1.empty()){
		cout<<s1.top()<<" ";
		s1.pop();
	}
	cout<<"size: "<<s1.size()<<endl;
}

int main(){

	tses1();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章