c++中queue头文件的使用

头文件

#include < queue >

定义

queue< int > a;
queue< string > s;

常用函数:

1 push() :在队列尾部插入元素
2 pop (): 移除最顶端的数据
3 size (): 输出队列中数据元素的个数
4 empty() : 判断队列是否为空
5 front ():返回队列中第一个元素,但是并不删除
6 back ():返回队列中最后一个元素,并且不删除

简单应用代码:

#include <iostream>
#include <queue> 
using namespace std;
int main(){
    
    
	queue<int> s;
	s.push(6);
	s.push(3);
	s.push(1);
	s.push(7);
	s.push(5);
	s.push(8);
	s.push(9);
	s.push(2);
	s.push(4);
	while(s.size()){
    
    
	cout<<s.front();
	s.pop();
	s.push(s.front());
	s.pop();
	}
	//	int a[101]={0,6,3,1,7,5,8,9,2,4};
//	int head=1;
//	int tail=10;
//	while(head<tail){
    
    
//		cout<<a[head]<<" ";
//		head++;
//		
//		a[tail]=a[head];
//		tail++;
//		head++;
//	
//	}
	return 0;
} 

结果:

615947283

猜你喜欢

转载自blog.csdn.net/weixin_45780132/article/details/112694769