STL之queue类

本文详细介绍了C++ STL中的`queue`和`priority_queue`。`queue`遵循先进先出(FIFO)原则,常用于需要顺序处理数据的场景。而`priority_queue`则是一种优先级最高的元素先出的数据结构,适用于需要根据优先级处理任务的情况。文中涵盖了它们的用法,包括头文件、定义方法和常见操作,并提供了实例解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

queue

本文讲queue和priority_queue的用法
一、解释:
队列也是一种逻辑数据结构,其具有先进先出的特性,针对这种特性,可以实现一些较为复杂的逻辑。在实际应用中,部分程序也正需要这样一种顺序进出的数据处理方式。使用这样的逻辑处理方式,使得我们可以将更多精力放在如何处理顺序逻辑之外的事情,对于编程、开发来讲,提供了极大的方便。
二、用法:
1.头文件

#include <queue>

2.定义方法

queue<int> q1;//定义一个成员类型为Int的空队列
queue <string> q2;//定义一个成员类型为string的空队列
queue <struct> q3;//定义一个成员类型为结构体的空队列

3.常用操作

    q1.push(x);//入队,将x接到队列的末端
    q1.pop();//出队,弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
    q1.front;//访问queue队首元素,即最早被压入队列的元素
    q1.back();//访问queue队尾元素,即最后被压入队列的元素
    q1.empty();//判断queue队列空,当队列空时,返回true
    q1.size();//访问队列中的元素个数
	q1=queue<int>();//因为queue没有clear()函数,这里提供第一种清空办法
	while(!q1.empty())q1.pop();//第二种清空办法
	void clear(queue<int>& q)//第三种最高效,定义一个clear()
	 {
   	 queue<int>empty;
     swap(empty,q);
  	 }

三、例子

### C++ STL Queue Usage and Examples In C++, `std::queue` is part of the Standard Template Library (STL), which provides a convenient way to implement queues using templates. A queue follows the First-In-First-Out (FIFO) principle where elements are inserted from one end called rear and deleted from another end known as front. To use `std::queue`, include `<queue>` header file. Here’s how one can define and initialize a queue: ```cpp #include <iostream> #include <queue> int main() { std::queue<int> q; } ``` Adding elements into the queue uses member function `push()` or `emplace()`. Removing elements employs `pop()`. Accessing but not removing the first element utilizes `front()`, while checking whether the queue is empty relies on `empty()`. To get the number of elements currently stored within the container adaptor, apply `size()` method[^1]. Below demonstrates basic operations with integers in a queue: ```cpp #include <iostream> #include <queue> void showQueue(std::queue<int> gq){ while (!gq.empty()){ std::cout << '\t' << gq.front(); gq.pop(); } } int main(){ std::queue<int> gquiz; gquiz.push(10); gquiz.push(20); std::cout << "The queue gquiz is : "; showQueue(gquiz); std::cout<<"\ngquiz.size() : "<<gquiz.size(); return 0; } ``` For more complex data structures like strings or custom objects, similar methods apply without modification needed since these functions work generically over different types through template instantiation mechanisms provided by C++. However, when dealing specifically with boolean values inside vectors (`std::vector<bool>`), special attention should be paid due to its unique implementation involving proxies rather than direct storage of bools [^2]. This does not affect standard queues directly unless such specialized containers become involved indirectly via contained elements.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值