http://msdn.microsoft.com/en-us/library/s1b6tszt(VS.80).aspx
Standard C++ Library Reference
queue Members
Reference
Constructors
queue
Constructs a queue that is empty or that is a copy of a base container object.
Typedefs
container_type
A type that provides the base container to be adapted by the queue.
size_type
An unsigned integer type that can represent the number of elements in a queue.
value_type
A type that represents the type of object stored as an element in a queue.
Member Functions
back
Returns a reference to the last and most recently added element at the back of the queue.
empty
Tests if the queue is empty.
front
Returns a reference to the first element at the front of the queue.
pop
Removes an element from the front of the queue.
push
Adds an element to the back of the queue.
size
Returns the number of elements in the queue.
Standard C++ Library Reference
queue Functions
Illustrates how to use the queue::push, queue::pop, queue::empty, queue::back, queue::front, and queue::size Standard Template Library (STL) functions in Visual C++.
queue::push( );
queue::pop( );
queue::empty( );
queue::back( );
queue::front( );
queue::size( );
Description
Note
The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.
The sample shows the queue implementation using list and deque containers.
Example
Copy Code
// queue.cpp
// compile with: /EHsc
//
// Functions:
// queue::push(), queue::pop(), queue::empty(), queue::back(),
// queue::front(),queue::size()
#include <list>
#include <iostream>
#include <queue>
#include <deque>
using namespace std ;
// Using queue with list
typedef list<int > INTLIST;
typedef queue<int> INTQUEUE;
// Using queue with deque
typedef deque<char*> CHARDEQUE;
typedef queue<char*> CHARQUEUE;
int main(void)
{
size_t size_q;
INTQUEUE q;
CHARQUEUE p;
// Insert items in the queue(uses list)
q.push(42);
q.push(100);
q.push(49);
q.push(201);
// Output the size of queue
size_q = q.size();
cout << "size of q is:" << size_q << endl;
// Output items in queue using front()
// and use pop() to get to next item until
// queue is empty
while (!q.empty())
{
cout << q.front() << endl;
q.pop();
}
// Insert items in the queue(uses deque)
p.push("cat");
p.push("ape");
p.push("dog");
p.push("mouse");
p.push("horse");
// Output the item inserted last using back()
cout << p.back() << endl;
// Output the size of queue
size_q = p.size();
cout << "size of p is:" << size_q << endl;
// Output items in queue using front()
// and use pop() to get to next item until
// queue is empty
while (!p.empty())
{
cout << p.front() << endl;
p.pop();
}
}
Output
size of q is:4
42
100
49
201
horse
size of p is:5
cat
ape
dog
mouse
horse
Requirements
Header: <queue>
Standard C++ Library Reference
queue::back
Returns a reference to the last and most recently added element at the back of the queue.
value_type& back( );
const value_type& back( ) const;
Return Value
The last element of the queue. If the queue is empty, the return value is undefined.
Remarks
If the return value of back is assigned to a const_reference, the queue object cannot be modified. If the return value of back is assigned to a reference, the queue object can be modified.
When compiling with _SECURE_SCL 1, a runtime error will occur if you attempt to access an element in an empty queue. See Checked Iterators for more information.
Example
Copy Code
// queue_back.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>
int main( )
{
using namespace std;
queue <int> q1;
q1.push( 10 );
q1.push( 11 );
int& i = q1.back( );
const int& ii = q1.front( );
cout << "The integer at the back of queue q1 is " << i
<< "." << endl;
cout << "The integer at the front of queue q1 is " << ii
<< "." << endl;
}
Output
The integer at the back of queue q1 is 11.
The integer at the front of queue q1 is 10.
Requirements
Header: <queue>
Standard C++ Library Reference
queue::push
Adds an element to the back of the queue.
void push(
const Type& _Val
);
Parameters
_Val
The element added to the top of the queue.
Remarks
The top of the queue is the position occupied by the most recently added element and is the last element at the end of the container.
Example
Copy Code
// queue_push.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>
int main( )
{
using namespace std;
queue <int> q1;
q1.push( 10 );
q1.push( 20 );
q1.push( 30 );
queue <int>::size_type i;
i = q1.size( );
cout << "The queue length is " << i << "." << endl;
i = q1.front( );
cout << "The element at the front of the queue is "
<< i << "." << endl;
}
Output
The queue length is 3.
The element at the front of the queue is 10.
Requirements
Header: <queue>
Standard C++ Library Reference
queue::pop
Removes an element from the front of the queue.
void pop( );
Remarks
The queue must be nonempty to apply the member function. The top of the queue is the position occupied by the most recently added element and is the last element at the end of the container.
Example
Copy Code
// queue_pop.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>
int main( )
{
using namespace std;
queue <int> q1, s2;
q1.push( 10 );
q1.push( 20 );
q1.push( 30 );
queue <int>::size_type i;
i = q1.size( );
cout << "The queue length is " << i << "." << endl;
i = q1.front( );
cout << "The element at the front of the queue is "
<< i << "." << endl;
q1.pop( );
i = q1.size( );
cout << "After a pop the queue length is "
<< i << "." << endl;
i = q1. front ( );
cout << "After a pop, the element at the front of the queue is "
<< i << "." << endl;
}
Output
The queue length is 3.
The element at the front of the queue is 10.
After a pop the queue length is 2.
After a pop, the element at the front of the queue is 20.
Requirements
Header: <queue>
Standard C++ Library Reference
queue::front
Returns a reference to the first element at the front of the queue.
value_type& front( );
const value_type& front( ) const;
Return Value
The first element of the queue. If the queue is empty, the return value is undefined.
Remarks
If the return value of front is assigned to a const_reference, the queue object cannot be modified. If the return value of front is assigned to a reference, the queue object can be modified.
The member function returns a reference to the first element of the controlled sequence, which must be nonempty.
When compiling with _SECURE_SCL 1, a runtime error will occur if you attempt to access an element in an empty queue. See Checked Iterators for more information.
Example
Copy Code
// queue_front.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>
int main() {
using namespace std;
queue <int> q1;
q1.push( 10 );
q1.push( 20 );
q1.push( 30 );
queue <int>::size_type i;
i = q1.size( );
cout << "The queue length is " << i << "." << endl;
int& ii = q1.back( );
int& iii = q1.front( );
cout << "The integer at the back of queue q1 is " << ii
<< "." << endl;
cout << "The integer at the front of queue q1 is " << iii
<< "." << endl;
}
Output
The queue
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
例子.rar (58个子文件)
栈和队列的 例子
stack2-0.cpp 367B
tree.plg 1KB
tree8.cpp 1KB
treeh.opt 48KB
treeh.dsp 3KB
search.plg 1KB
class 使用说明.txt 7KB
queue.cpp 437B
in1.dat 31B
stack1.cpp 381B
tree.dsp 3KB
stack3.cpp 384B
maze1.cpp 1KB
level.cpp 1KB
stackb.cpp 266B
tree.opt 48KB
queue0.cpp 630B
treeh.dsw 535B
stack4.cpp 457B
bfstree4.cpp 1KB
queue class 使用说明.txt 9KB
search1.cpp 999B
stack.cpp 275B
stack2.cpp 378B
stack2-1.cpp 380B
Debug
treeh.pdb 1.04MB
vc60.pdb 108KB
treeh.obj 241KB
search.pdb 1.03MB
tree.obj 241KB
treeh.exe 512KB
search.exe 504KB
search.obj 188KB
tree.pdb 1.04MB
tree.exe 512KB
maze.cpp 1KB
tree.cpp 800B
search.opt 48KB
stack0.cpp 611B
in2.dat 28B
bfstree8.cpp 1KB
search.dsw 537B
treeh.cpp 883B
maze1.in 223B
tree.dsw 533B
treeh.plg 1KB
stack2-2.cpp 368B
stack1-1.cpp 388B
search.cpp 955B
tree4.cpp 924B
in.dat 31B
maze.in 71B
search.ncb 33KB
bfstree.cpp 930B
tree.ncb 33KB
queue2.cpp 436B
treeh.ncb 33KB
search.dsp 3KB
共 58 条
- 1
资源评论
- jidsfw1432014-04-15不错,很好的样子
hylhuang
- 粉丝: 1
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功