C++ 中queue(队列)的用法

#include <iostream>
#include <queue>
#include <assert.h>
/*
调用的时候要有头文件: #include<stdlib.h> 或

#include<cstdlib> +

#include<queue>       #include<queue>
详细用法:
定义一个queue的变量     queue<Type> M
查看是否为空范例        M.empty()    是的话返回1,不是返回0;
从已有元素后面增加元素   M.push()
输出现有元素的个数      M.size()
显示第一个元素          M.front()
显示最后一个元素        M.back()
清除第一个元素          M.pop()
*/
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
queue <int> myQ;

cout<< "现在 queue 是否 empty? "<< myQ.empty() << endl; 

for(int i =0; i<10 ; i++)
{
myQ.push(i);
}
for(int i=0; i<myQ.size(); i++)
{
printf("myQ.size():%d\n",myQ.size());
cout << myQ.front()<<endl;
myQ.pop();
}

system("PAUSE"); 

return 0;
}

输出结果:
现在 queue 是否 empty? 1
myQ.size():10
0
myQ.size():9
1
myQ.size():8
2
myQ.size():7
3
myQ.size():6
4
请按任意键继续. . .

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在C语言,标准库并没有内置的队列queue)库。如果您使用了第三方队列库,那么具体的查看队列长度函数会依赖于您使用的库。 下面是一些常见的第三方队列库及其查看队列长度的函数: 1. C++ STL队列queue)库:如果您在C++使用STL的队列库,可以通过 `size()` 函数查看队列元素的个数,例如: ``` #include <queue> using namespace std; queue<int> q; int len = q.size(); // 获取队列长度 ``` 2. C 语言的 `queue.h` 库:如果您使用的是C 语言的 `queue.h` 库,可以通过 `queue_size()` 函数查看队列元素的个数,例如: ``` #include <stdio.h> #include <stdlib.h> #include <queue.h> int main() { QUEUE *q = que_init(); que_push(q, 1); que_push(q, 2); que_push(q, 3); int len = queue_size(q); // 获取队列长度 printf("Queue size: %d\n", len); que_free(q); return 0; } ``` 请注意,以上示例代码仅供参考,具体实现可能会因库的版本或个人编写方式而有所不同。 ### 回答2: C语言queue库并不是标准库的一部分,它是一个由用户自己实现的队列库。因此,并没有一个固定的函数来查看队列的长度。 用户可以通过在队列结构体添加一个表示队列当前长度的变量来实现此功能。在进行入队操作时,该变量加1;在出队操作时,该变量减1。这样,通过直接访问该变量即可得到队列的当前长度。 下面是一个示例代码: ``` #include <stdio.h> #define MAX_SIZE 100 // 定义队列的最大容量 typedef struct { int data[MAX_SIZE]; // 用数组来存储队列元素 int front; // 队首指针 int rear; // 队尾指针 int length; // 队列长度 } Queue; // 初始化队列 void initQueue(Queue *queue) { queue->front = 0; queue->rear = -1; queue->length = 0; } // 入队操作 void enqueue(Queue *queue, int element) { if (queue->length == MAX_SIZE) { printf("Queue is full!\n"); return; } queue->rear = (queue->rear + 1) % MAX_SIZE; queue->data[queue->rear] = element; queue->length++; } // 出队操作 int dequeue(Queue *queue) { if (queue->length == 0) { printf("Queue is empty!\n"); return -1; } int element = queue->data[queue->front]; queue->front = (queue->front + 1) % MAX_SIZE; queue->length--; return element; } // 查看队列长度 int getQueueLength(Queue *queue) { return queue->length; } int main() { Queue queue; initQueue(&queue); enqueue(&queue, 1); enqueue(&queue, 2); enqueue(&queue, 3); printf("Queue length: %d\n", getQueueLength(&queue)); dequeue(&queue); printf("Queue length: %d\n", getQueueLength(&queue)); return 0; } ``` 以上代码实现了一个队列的基本操作,并通过`getQueueLength`函数获取队列的长度。 ### 回答3: cqueue库的查看队列长度的函数是`size()`函数。 `size()`函数用于返回队列的元素个数。调用方法如下: ``` #include <stdio.h> #include <stdbool.h> #include <stdlib.h> #include <queue.h> int main() { queue *q = queue_creare(); // 向队列添加元素 queue_push(q, 1); queue_push(q, 2); queue_push(q, 3); // 查看队列的长度 int length = size(q); printf("队列长度为:%d\n", length); // 释放队列 queue_destroy(q); return 0; } ``` 在上述例子,首先使用`queue_create()`函数创建了一个空队列`q`,然后使用`queue_push()`函数向队列添加了三个元素,接着使用`size()`函数获取了队列的长度并存储在变量`length`,最后使用`printf()`函数将队列长度输出到屏幕上。最后使用`queue_destroy()`函数释放了队列所占用的内存。 这样就可以通过`size()`函数获取队列的长度了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值