C++ 里面 系统自带的队列类queue 怎么用?
1需要什么头文件来包含?2这个类里面都有什么方法,怎么用?3帮我把下面代码中关于queue的方法名修改正确。voidGraph::BFS(intu,int*parent,...
1需要什么头文件来包含?
2这个类里面都有什么方法,怎么用?
3帮我把下面代码中关于queue的方法名修改正确。
void Graph::BFS(int u,int* parent,ColorType *color)
{
queue<int> q(QSize);
color[u]=Gray; //标记起始结点u为活结点
cout<<""<<u;
q.push(u); //将起始结点u加入队列q
while (!q.empty())
{
u=q.front();q.Serve(); //选择一个活结点为E-结点
for (ENode* w=a[u];w;w=->nextArc)//检测E-结点u的全部邻接点
{
int v=w->adjVex;
if (color[v]==White)
{
color[v]=Gray;
cout<<" "<<v;
parent[v]=u; //构造BFS生成森林
q.Append(v); //新的活结点进入活结点表q
}
}
color[u]=Black; //标记死结点
}
} 展开
2这个类里面都有什么方法,怎么用?
3帮我把下面代码中关于queue的方法名修改正确。
void Graph::BFS(int u,int* parent,ColorType *color)
{
queue<int> q(QSize);
color[u]=Gray; //标记起始结点u为活结点
cout<<""<<u;
q.push(u); //将起始结点u加入队列q
while (!q.empty())
{
u=q.front();q.Serve(); //选择一个活结点为E-结点
for (ENode* w=a[u];w;w=->nextArc)//检测E-结点u的全部邻接点
{
int v=w->adjVex;
if (color[v]==White)
{
color[v]=Gray;
cout<<" "<<v;
parent[v]=u; //构造BFS生成森林
q.Append(v); //新的活结点进入活结点表q
}
}
color[u]=Black; //标记死结点
}
} 展开
展开全部
queue 是 STL 中的一个容器,就是队列,特点是先进先出
一个例子你几乎就能了解它的大部分功能了:
int main()
{
std::queue<int> q;
q.push( 1 );
q.push( 2 );
q.push( 3 );
while (!q.empty())
{
cout << q.front() << endl;
q.pop();
}
return 0;
};
用到了queue的 4 个操作:
push() 入队列
empty() 队列是否为空
front() 队列的头
pop() 出队列(先进先出,当然是头出了)
完.
一个例子你几乎就能了解它的大部分功能了:
int main()
{
std::queue<int> q;
q.push( 1 );
q.push( 2 );
q.push( 3 );
while (!q.empty())
{
cout << q.front() << endl;
q.pop();
}
return 0;
};
用到了queue的 4 个操作:
push() 入队列
empty() 队列是否为空
front() 队列的头
pop() 出队列(先进先出,当然是头出了)
完.
2015-08-09
展开全部
参考使用方法如下:
class MyQueue
{
//存放元素的数组
private object[] _array;
//增长因子
private int _growFactor;
//队头下标
private int _head;
//队尾下标
private int _tail;
private int _size;
private const int _MinGrow = 4;
//初始容量
private const int _ShrikThreadhold = 0x20;
public MyQueue()
: this(_ShrikThreadhold, 2f)
{
}
public MyQueue(int capacity, float growFactor)
{
if (capacity < 0)
{
throw new ArgumentOutOfRangeException("capacity", "初始容量不能为负!");
}
if ((growFactor < 1.0) || (growFactor > 10))
{
throw new ArgumentOutOfRangeException("growFactor", "增长因子应介于1和10之间!");
}
this._array = new object[capacity];
this._head = 0;
this._size = 0;
this._tail = 0;
this._growFactor = (int)(growFactor * 100f);
}
//入队
public virtual void Enqueue(object obj)
{
if (this._size == this._array.Length)
{//队列已满
int capacity = (int)((this._array.Length * this._growFactor) / 100L);
if (capacity < (this._array.Length + _MinGrow))
{
capacity = this._array.Length + _MinGrow;
}
SetCapacity(capacity);
}
this._array[this._tail] = obj;
this._tail = (this._tail + 1) % this._array.Length;
this._size++;
}
//内存设置
private void SetCapacity(int capacity)
{
object[] dest = new object[capacity];
if (this._head < this._tail)//尾指针在头指针后
{
Array.Copy(this._array, this._head, dest, 0, this._size);
}
else//头指针在尾指针后
{
Array.Copy(this._array, this._head, dest, 0, this._array.Length - this._head);
Array.Copy(this._array, 0, dest, this._array.Length - this._head, this._tail);
}
this._array = dest;
this._head = 0;
this._tail = (this._size == capacity) ? 0 : this._size;
}
//出队
public virtual object Dequeue()
{
if (this._size == 0)
{
throw new InvalidOperationException("队列为空");
}
object obj = this._array[this._head];//出队
this._array[this._head] = null;
this._head = (this._head + 1) % this._array.Length;
this._size--;
return obj;
}
public virtual int Count
{
get { return this._size; }
}
}
static void Main(string[] args)
{
Program main = new Program();
main.testQueue();
}
private void testQueue()
{
Queue<int> myQueue = new Queue<int>();
//MyQueue myQueue = new MyQueue();//与上面类使用一样
myQueue.Enqueue(1);
myQueue.Enqueue(2);
myQueue.Enqueue(3);
myQueue.Enqueue(4);
Console.WriteLine("总数量:"+myQueue.Count.ToString());
while (myQueue.Count > 0)
{
Console.WriteLine("出列:"+myQueue.Dequeue());
}
Console.WriteLine("数量:"+myQueue.Count.ToString());
Console.ReadLine();
}
class MyQueue
{
//存放元素的数组
private object[] _array;
//增长因子
private int _growFactor;
//队头下标
private int _head;
//队尾下标
private int _tail;
private int _size;
private const int _MinGrow = 4;
//初始容量
private const int _ShrikThreadhold = 0x20;
public MyQueue()
: this(_ShrikThreadhold, 2f)
{
}
public MyQueue(int capacity, float growFactor)
{
if (capacity < 0)
{
throw new ArgumentOutOfRangeException("capacity", "初始容量不能为负!");
}
if ((growFactor < 1.0) || (growFactor > 10))
{
throw new ArgumentOutOfRangeException("growFactor", "增长因子应介于1和10之间!");
}
this._array = new object[capacity];
this._head = 0;
this._size = 0;
this._tail = 0;
this._growFactor = (int)(growFactor * 100f);
}
//入队
public virtual void Enqueue(object obj)
{
if (this._size == this._array.Length)
{//队列已满
int capacity = (int)((this._array.Length * this._growFactor) / 100L);
if (capacity < (this._array.Length + _MinGrow))
{
capacity = this._array.Length + _MinGrow;
}
SetCapacity(capacity);
}
this._array[this._tail] = obj;
this._tail = (this._tail + 1) % this._array.Length;
this._size++;
}
//内存设置
private void SetCapacity(int capacity)
{
object[] dest = new object[capacity];
if (this._head < this._tail)//尾指针在头指针后
{
Array.Copy(this._array, this._head, dest, 0, this._size);
}
else//头指针在尾指针后
{
Array.Copy(this._array, this._head, dest, 0, this._array.Length - this._head);
Array.Copy(this._array, 0, dest, this._array.Length - this._head, this._tail);
}
this._array = dest;
this._head = 0;
this._tail = (this._size == capacity) ? 0 : this._size;
}
//出队
public virtual object Dequeue()
{
if (this._size == 0)
{
throw new InvalidOperationException("队列为空");
}
object obj = this._array[this._head];//出队
this._array[this._head] = null;
this._head = (this._head + 1) % this._array.Length;
this._size--;
return obj;
}
public virtual int Count
{
get { return this._size; }
}
}
static void Main(string[] args)
{
Program main = new Program();
main.testQueue();
}
private void testQueue()
{
Queue<int> myQueue = new Queue<int>();
//MyQueue myQueue = new MyQueue();//与上面类使用一样
myQueue.Enqueue(1);
myQueue.Enqueue(2);
myQueue.Enqueue(3);
myQueue.Enqueue(4);
Console.WriteLine("总数量:"+myQueue.Count.ToString());
while (myQueue.Count > 0)
{
Console.WriteLine("出列:"+myQueue.Dequeue());
}
Console.WriteLine("数量:"+myQueue.Count.ToString());
Console.ReadLine();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
标准库是头文件 #include<queue>
方法很多 其他的都对 但是
q.Serve()q.Append()没有
方法很多 其他的都对 但是
q.Serve()q.Append()没有
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <queue>
using namespace std;
void main(){
queue<int> qint;
qint.push_back();
qint.pop();
qint.top();
//以及其它增,删,改操作
}
using namespace std;
void main(){
queue<int> qint;
qint.push_back();
qint.pop();
qint.top();
//以及其它增,删,改操作
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询