绪:
看法式碰到queue队列的利用方式;
下面经由过程一个例程来简要概述一下queue队列的常识点:
什么是队列;挨次队列;
C++:queue队列的用法;
模板类;

东西/原料
- Visual Studio 2010
方式/步调
- 1
queue应用例程:
#include <queue>
#include <iostream>
using namespace std;
int main()
{
queue<int> myQ;
for(int i=0; i<10; i++)
myQ.push(i);
cout<<"myQ size is: "<<myQ.size()<<endl;
for(int i=0; i<myQ.size(); i++)
{
cout << myQ.front()<<endl;
myQ.pop();
}
cout<<"myQ size is: "<<myQ.size()<<endl;
return 0;
}
- 2
什么是队列?
队列是一种特别的线性表,
特别之处在于:它只许可在表的前端进行删除操作,只许可在表的后端进行插入操作;
队列是一种操作受限制的线性表;
进行插入操作的端称为队从头至尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。
队列的数据元素又称为队列元素。
在队列中插入一个队列元素称为入队,
从队列中删除一个队列元素称为出队。
因为队列只许可在一端插入,在另一端删除,所以只有最早进入队列的元素才能最先从队列中删除,故队列又称为进步前辈先出(FIFO—first in first out)线性表。
- 3
挨次队列:
成立挨次队列布局必需为其静态分派或动态申请一片持续的存储空间,并设置两个指针进行办理。
一个是队头指针front,它指标的目的队头元素;
另一个是队从头至尾指针rear,它指标的目的下一个入队元素的存储位置,
如图所示:
- 4
C++中的queue:
queue是STL的队列,有FIFO的特征。
①队列头文件:#include <queue>
②queue模板类:需要两个模板参数,
一个是元素类型,一个容器类型,
元素类型是需要的,容器类型是可选的,默认为deque类型。
界说queue对象的示例代码如下:
queue<int> q1;
queue<double> q2;
queue<Point> q3;
- 5
queue的根基操作有:
入队,如例:q.push(x); 将x接到队列的结尾。
出队,如例:q.pop(); 弹出队列的第一个元素,注重,并不会返回被弹出元素的值。
拜候队首元素,如例:q.front(),即最早被压入队列的元素。
拜候队从头至尾元素,如例:q.back(),即最后被压入队列的元素。
判定队列空,如例:q.empty(),当队列空时,返回true。
拜候队列中的元素个数,如例:q.size()
- 6
queue的应用:
#include "stdafx.h"
#include <queue>
#include <iostream>
#include <string>
using namespace std;
void test_empty()
{
queue<int> myqueue;
int sum (0);
for (int i=1;i<=10;i++)
myqueue.push(i);
while (!myqueue.empty())
{
sum += myqueue.front();
myqueue.pop();
}
cout << "total: " << sum << endl;
}
void test_pop()
{
queue<int> myqueue;
int myint;
cout << "\nPlease enter some integers (enter 0 to end):\n";
do
{
cin >> myint;
myqueue.push (myint);
} while (myint);
cout << "myqueue contains: ";
while (!myqueue.empty())
{
cout << " " << myqueue.front();
myqueue.pop();
}
}
void test_size()
{
queue<int> myints;
cout << "0. size: " << (int) myints.size() << endl;
for (int i=0; i<5; i++) myints.push(i);
cout << "1. size: " << (int) myints.size() << endl;
myints.pop();
cout << "2. size: " << (int) myints.size() << endl;
}
int main()
{
test_empty();
cout<<"\n***********************************************\n";
test_size();
cout<<"\n***********************************************\n";
test_pop();
cout<<"\n***********************************************\n";
queue<string> q;
// insert three elements into the queue
q.push("These ");
q.push("are ");
q.push("more than ");
//cout << "number of elements in the queue: " << q.size()<< endl;
// read and print two elements from the queue
cout << q.front();
q.pop();
cout << q.front();
q.pop();
//cout << "number of elements in the queue: " << q.size()<< endl;
// insert two new elements
q.push("four ");
q.push("words!");
// skip one element
q.pop();
// read and print two elements
cout << q.front();
q.pop();
cout << q.front() << endl;
q.pop();
cout << "number of elements in the queue: " << q.size()<< endl;
return 0;
}
注重事项
- q.push(x); 将x接到队列的结尾
- q.pop(); 弹出队列的第一个元素,注重,并不会返回被弹出元素的值。
- q.front()拜候首元素;
- q.back()拜候从头至尾元素;
来源:百闻(微信/QQ号:9397569),转载请保留出处和链接!
本文链接:https://www.ibaiwen.com/web/236464.html
- 上一篇: 怎么使用酷狗音乐进行K歌
- 下一篇: windows如何安装appium
- 热门文章
-
WB蒙特利尔(WB Montreal)——欧美十大最差视频游戏开发商
迅猛龙(Velociraptor)——欧美史前十大死亡动物
什么是果酱猫(What Marmalade Cats)?
神奇蜘蛛侠2(The Amazing Spider-Man 2)——欧美最佳蜘蛛侠电影
希瑟(Heather)——欧美十大最佳柯南灰歌
二人梭哈
faceu激萌怎么把瘦脸开到最大
奥兹奥斯本(Ozzy Osbourne)——欧美十大高估歌手
什么是小脑前下动脉(Anterior Inferior Cerebellar Artery)?
我应该知道康涅狄格州的什么(What Should I Know About Connecticut)?
- 热评文章
- 最新评论
-
- 最近访客
-
- 站点信息
-
- 文章总数:200248
- 页面总数:9
- 分类总数:1
- 标签总数:0
- 评论总数:0
- 浏览总数:497