C++中栈和队列的使用方法(satck和queue)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wx1458451310/article/details/88043575

栈(stack):

#include <iostream>
#include <stack>

using namespace std;

int main()
{
	stack<int> s;
	if(s.empty())
		cout<<"empty"<<endl;   //empty
	s.push(1);
	s.push(6);
	s.push(66);
	cout<<s.size()<<endl;   //3
	s.pop();
	cout<<s.size()<<endl;	//2
	cout<<s.top()<<endl;	//6
	cout<<s.size()<<endl;	//2
	system("pause");
	return 0;

}

队列(queue):

#include <iostream>
#include <queue>

using namespace std;

int main()
{
	queue<int> q;
	if(q.empty())
		cout<<"empty"<<endl;	//empty
	q.push(1);
	q.push(6);
	q.push(66);

	cout<<q.front()<<endl;	//1
	cout<<q.size()<<endl;	//3
	q.pop();

	cout<<q.size()<<endl;	//2

	q.push(10);
	cout<<q.back()<<endl;	//10

	cout<<q.front()<<endl;	//6

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wx1458451310/article/details/88043575