C++标准程序库 学习笔记 第十章 特殊容器

特殊容器: stack 栈 queue 队列 priority_queue优先队列~      特殊容器没有迭代器的哟~~


1. stack 栈   默认由deque实作而成~    主要是用 push() pop() top() 三个成员函数

/*
author: wzy1222 ;
email: 627440781@qq.com
for: stack
*/
#include <iostream>
#include <stack>
#include <deque>
#include <vector>
using namespace std;
int main()
{
	int arr[]={1,2,3,4,5};
	deque<int> ideq(arr,arr+5);
	// stack 是由 deque 实作而来,初始化可以直接赋值~ vector就不行了~
	stack<int> stk(ideq);
	cout<< stk.top() <<endl;

	// stack由什么实作而成,就可以由什么来初始化~
	vector<int> ivec(arr,arr+5);
	stack<int,vector<int> > stk22(ivec);
	cout<<stk22.top()<<endl;

	stk.push(100);
	stk.push(200);
	stk.top()=1000;
	while( !stk.empty())
	{
		cout<<stk.top()<<endl;
		stk.pop();
	}
	system("PAUSE");
	return 0;
}

2 queue 队列  由deque实作而成  主要有 push() pop() back() front() 四个成员函数

/*
author: wzy1222 ;
email: 627440781@qq.com
for: queue
*/
#include <iostream>
#include <deque>
#include <queue>
using namespace std;
int main()
{
	int arr[]={1,2,3,4,5};
	deque<int> dqe(arr,arr+5);
	// queue 也是由deque实作而成~~ 
	queue<int> que(dqe);

	cout<<"back: "<<que.back()<<endl;
	cout<<"front "<<que.front()<<endl; 
	que.push(100);
	cout<<"push 100: "<<"que.back()= "<<que.back()<<endl;
	que.pop();
	cout<<"que.pop "<<"que.front()= "<<que.front()<<endl;

	system("PAUSE");
	return 0;
}

3 priority_queue 优先队列 由vector实作而成,主要有  push() top() pop()成员函数

/*
author: wzy1222 ;
email: 627440781@qq.com
for: priority_queue
*/
#include <iostream>
#include <queue>
using namespace std;
int main()
{
	priority_queue<double> pque;
	pque.push(50);
	pque.push(100);
	pque.push(25);

	cout<<"top = "<< pque.top()<<endl;
	cout<<endl;
	while( !pque.empty())
	{
		cout<<pque.top()<<endl;
		pque.pop();
	}
	system("PAUSE");
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值