c++ STL容器隊列queue

c++STL學習視頻
https://www.bilibili.com/video/av63384955?from=search&seid=5013240884410317680

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

#include<iostream>
#include <queue>
using namespace std;
class  teacher
{
	
	
	public :		
	int age;
	char name[32];
		void printf()
		{
			cout<<age<<endl;
		}
};

void int_queue()
{
	queue <int> q;
	q.push(1);
	q.push(2);
	q.push(3); //push(元素)
    q.front() ;
    q.size();
    while(!q.empty())
	{
		cout<<q.front()<<endl;
		q.pop();
	}	
 } 
 void teacher_queue()
 {
 	teacher t1,t2,t3;
	t1.age=31;
	t2.age=19;
	t3.age=45;
 	queue <teacher> q; //隊列創建
 	q.push(t1);//入隊
 	q.push(t2);
 	q.push(t3);
 	while(!q.empty()) 
 	{  
 	    teacher tmp;
 		tmp=q.front();//出隊但不刪除
 		tmp.printf();
 		q.pop();//從隊列中刪除 
	 }
 	
 }
 
  void teacher_queue2()
 {
 	teacher t1,t2,t3;
	t1.age=31;
	t2.age=19;
	t3.age=45;
 	queue <teacher*> q;
 	q.push(&t1);
 	q.push(&t2);
 	q.push(&t3);
 	while(!q.empty()) 
 	{  
 	    teacher* tmp;
 		tmp=q.front();//出來的不是元素,而是元素的地址  
 		tmp->printf();//指針不能用.用->訪問  
 		q.pop();
	 }
 	
 }
 int main()
 {
 	
 int_queue();
 teacher_queue();
 teacher_queue2();
 return 0; 
 } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章