8.3C++:插入迭代器、顺序容器的适配器、栈和队列、优先级队列

1 插入迭代器

在这里插入图片描述
插入迭代器参数是容器

back_iterator iter(s); //将iter定义为容器s的后插迭代器
*iter = 5; //相当于在尾部插入5

2 顺序容器的适配器

通过顺序容器形成的数据结构
在这里插入图片描述
3 栈和队列

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <string>
#include <stack>
using namespace std;

int main()
{
    stack<char> s;
    string str;  
    cin >> str;  //键盘输入一个字符串
    //将字符串的每个元素顺序入栈
    for (string::iterator iter = str.begin(); iter != str.end(); iter++)
        s.push(*iter);
    while (!s.empty()) {
        cout << s.top();
        s.pop();
    }
    cout << endl;
    return 0;
}
输入:happy
输出:yppah

4 优先级队列

#include <iostream>
#include <queue> //queue和priority_queue包含在头文件queue中
#include <typeinfo>
using namespace std;

const int SPLIT_TIME_MIN = 500;
const int SPLIT_TIME_MAX = 2000;

class Cell;
priority_queue <Cell> cellQueue;

class Cell {
	static int count; //细胞总数
	int id;
	int time;
public:
	Cell(int birth) : id(count++) {
		time = birth + (rand() % (SPLIT_TIME_MAX - SPLIT_TIME_MIN)) + SPLIT_TIME_MIN;
	}
	int getID() const { return id; }
	int getSplitTime() const { return time; }
	bool operator < (const Cell& s) const {  //cell的“<”表示细胞更加新,更靠后分裂,因此time更大
		//重载"<“帮助优先队列识别最高优先级元素,将更大的(也就是time更小的)细胞置于更高优先级
		return time > s.time;
	}
	void split() const{   //为什么要加const
		Cell child1(time), child2(time);
		cout << time << "s:Cell #" << id << "splits to #"
			<< child1.getID() << "and #" << child2.getID() << endl;
		cellQueue.push(child1); //将2个子细胞压入优先级队列中
		cellQueue.push(child2);
	}
};
int Cell::count = 0;
int main() {
	cout << typeid(time(0)).name() << endl; //  time(0)是__int64类型,返回当前系统时间
	srand(static_cast<unsigned int>(time(0))); //srand(unsigned int seed),产生随机数种子
	// static_cast<unsigned int>与static_cast<unsigned>完全相同,是强制类型转换,将time(0)转换为unsigened类型
	int t;
	cout << "输入模拟时间";
	cin >> t;
	cellQueue.push(Cell(0));
	while (cellQueue.top().getSplitTime() < t) {  //top为队列的队头元素
		cellQueue.top().split();  //top()是最早进入队列的元素
		cellQueue.pop();  //弹出队列最先被压入的元素   
	}
	while (!cellQueue.empty()) {  //看看后面的队列排序
		cout << cellQueue.top().getID() << "  ";
		cellQueue.pop();
	}
	return 0;
}
输出:
__int64
输入模拟时间5000
1459s:Cell #0splits to #1and #2
2296s:Cell #1splits to #3and #4
3033s:Cell #2splits to #5and #6
3847s:Cell #4splits to #7and #8
4075s:Cell #5splits to #9and #10
4220s:Cell #3splits to #11and #12
4443s:Cell #6splits to #13and #14
4665s:Cell #7splits to #15and #16
4712s:Cell #8splits to #17and #18
12  17  18  10  14  15  11  13  9  16

优先级队列cellQueue的队头元素是12,由于其time大于5000,所以终止细胞分裂。

由于每个细胞的分裂时间及其祖先细胞的分裂时间是随机数,所以细胞在优先级队列中的顺序并不与id直接相关。例如17从4712经过600开始分裂,17的time是5312;反而11从4220经过1500开始分裂,11的time是5720;11排在17后面。

优先级队列的基本操作和队列基本操作相同:
top 访问队头元素
empty 队列是否为空
size 返回队列内元素个数
push 插入元素到队尾 (并排序)
emplace 原地构造一个元素并插入队列
pop 弹出队头元素
swap 交换内容

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值