队列的构造以及使用方法(the usage and construction of queue)
2012-12-31 11:25
525 查看
队列(queue):由容器的名字就可以知道,队列就是把一排元素排成一队,所以队列有先进先出的原则,即先来的站前边,后到的站后边;
插入/删除:只允许从队尾插入,允许删除的为队头;(即每次离开的都为最老的“队友”);
空队列:当队列中没有元素时称为空队列;
队列主要应用于简单的搜索中,因此熟练应用队列是进行搜索操作的基本条件;
队列的构造:
1 首先要声明头文件#include<queue>
2 定义格式 queue<int>q (int代表的存入队列的数据元素类型,q是该队列的名字)
队列的成员函数:
1 size 返回队列的元素个数
2 empty 判断队列是否为空
3 front 返回队头元素的值
4 back 返回队尾元素的值
5 pop 移除最老的那位元素,即移除队头元素
6 push 添加一个元素到队尾
队列使用案例:
插入/删除:只允许从队尾插入,允许删除的为队头;(即每次离开的都为最老的“队友”);
空队列:当队列中没有元素时称为空队列;
队列主要应用于简单的搜索中,因此熟练应用队列是进行搜索操作的基本条件;
队列的构造:
1 首先要声明头文件#include<queue>
2 定义格式 queue<int>q (int代表的存入队列的数据元素类型,q是该队列的名字)
队列的成员函数:
1 size 返回队列的元素个数
2 empty 判断队列是否为空
3 front 返回队头元素的值
4 back 返回队尾元素的值
5 pop 移除最老的那位元素,即移除队头元素
6 push 添加一个元素到队尾
队列使用案例:
// the usage of queue #include<iostream> #include<queue> using namespace std; int main() { queue<int>q; int i; // size/push for(i=1;i<=10;i++) { q.push(i); } int len=q.size(); cout<<len<<endl; //front/back q.front()-=q.back();//1-10 cout<<"q.front()="<<q.front()<<endl; //empty/pop int sum=0; while(!q.empty()) { sum+=q.front(); q.pop(); } cout<<"sum="<<sum<<endl; int len2=q.size(); cout<<"len2="<<len2<<endl; return 0; }
相关文章推荐
- 优先队列的构造以及使用方法的总结(for priority_queue)
- The Java™ Tutorials — Generics :Effects of Type Erasure and Bridge Methods 类型擦除的影响以及桥方法
- map(映射)的使用方法 (The usage of map.)
- STL 中优先队列的使用方法(priority_queue)
- priority_queue 优先队列使用方法
- Codeforces Round #251(Div. 2) 439C. Devu and Partitioning of the Array 构造
- [React] Create a queue of Ajax requests with redux-observable and group the results.
- 构造方法,成员方法的区别以及使用
- 优先队列priority_queue的使用方法
- Java使用Rabbitmq惊喜队列queue和消息内容的本地持久化核心方法。(内容存储在硬盘)
- .Net EF Core数据库使用SQL server 2008 R2分页报错How to avoid the “Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement.”
- The installation and usage of Auto-jump and ZSH
- STL中队列(queue)的使用方法
- java中PriorityQueue优先级队列使用方法
- java中PriorityQueue优先级队列使用方法
- java中PriorityQueue优先级队列使用方法
- linux内核的 等待队列 使用方法,wait_queue_head_t,进程休眠
- java中PriorityQueue优先级队列使用方法
- linux内核的 等待队列 使用方法,wait_queue_head_t,进程休眠
- C++ 优先队列 priority_queue 的基本使用方法【定义优先级】