STL的queue

该博客围绕数据结构中的队列展开,虽无具体内容,但可知队列是核心。队列是重要的数据结构,在信息技术领域有广泛应用。
#include <iostream>
#include <queue>
using namespace std;

int main(int argc, char** argv) 
{
	queue<int>q;
	
	q.push(10);
	q.push(20);
	q.push(30);
	cout<<q.front()<<endl;//读取队首元素 
	cout<<q.size()<<endl;//队列的大小 
	cout<<q.back()<<endl;//读取队尾元素 
	q.pop();//弹出队头元素 
	cout<<q.front()<<endl;
	cout<<q.empty()<<endl;//判断是否为空 
	return 0;
}
### 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.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值