Defined in the header file <queue>
template< class T, |
std::queue
Classes are container adapters that give the programmer the functionality of queues -- specifically FIFO (first-in-first-out) data structures.
A class template acts as a wrapper around the underlying container - providing only a specific set of functions. The queue pushes elements at the end of the underlying container and pops elements at the beginning.
modifier
Insert an element at the end of the queue
std::queue<T,Container>::push
void push( const value_type& value ); |
||
void push( value_type&& value ); |
(since C++11) |
Push the given element value
to the end of the queue.
1) Equivalently call c.push_back(value)
2) Equivalently call c.push_back(std::move(value))
parameter
value | - | the element value to push |
return value
(none)
the complexity
Equal to the complexity of Container::push_back.
Construct elements in situ at the tail
std::queue<T,Container>::emplace
template< class... Args > |
(since C++11) (until C++17) |
|
template< class... Args > |
(since C++17) |
Pushes a new element to the end of the queue. Constructs the element in-place, that is, no move or copy operations are performed. Calls the element's constructor with exactly the same arguments that were supplied to the function.
Equivalently calls c.emplace_back(std::forward<Args>(args)...); .
parameter
args | - | Parameters forwarded to the element's constructor |
return value
(none) | (before C++17) |
The value or reference returned by the above call to Container::emplace_back, if it exists. | (since C++17) |
the complexity
Equivalent to the complexity of Container::emplace_back.
remove the top element of the stack
std::queue<T,Container>::pop
void pop(); |
Removes the front element from the queue. Equivalently calls c.pop_front().
parameter
(none)
return value
(none)
the complexity
Equal to the complexity of Container::pop_front.
exchange content
std::queue<T,Container>::swap
void swap( queue& other ) noexcept(/* see below */); |
(since C++11) |
Swaps the content other
of . Equivalently calls using std::swap; swap(c, other.c); .
parameter
other | - | The container adapter to swap content with |
return value
(none)
example
noexcept specifies: noexcept(noexcept(swap(c, other.c))) In the above expression, the identifier is looked up in the same way as used by the C++17 std::is_nothrow_swappable attribute |
(before C++17) |
noexcept specifies: noexcept(std::is_nothrow_swappable<Container>::value) |
(since C++17) |
the complexity
Same as the underlying container (typically a constant).
call example
#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <queue>
#include <deque>
#include <time.h>
using namespace std;
struct Cell
{
int x;
int y;
Cell() = default;
Cell(int a, int b): x(a), y(b) {}
Cell &operator +=(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator +(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator *(const Cell &cell)
{
x *= cell.x;
y *= cell.y;
return *this;
}
Cell &operator ++()
{
x += 1;
y += 1;
return *this;
}
bool operator <(const Cell &cell) const
{
if (x == cell.x)
{
return y < cell.y;
}
else
{
return x < cell.x;
}
}
bool operator >(const Cell &cell) const
{
if (x == cell.x)
{
return y > cell.y;
}
else
{
return x > cell.x;
}
}
bool operator ==(const Cell &cell) const
{
return x == cell.x && y == cell.y;
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
void queuePrint(const std::string &name, const std::queue<Cell> &queue)
{
std::cout << name ;
std::queue<Cell> queuep = queue;
while (queuep.size() > 0)
{
std::cout << queuep.front() << " ";
queuep.pop();
}
std::cout << std::endl;
}
int main()
{
std::cout << std::boolalpha;
std::mt19937 g{std::random_device{}()};
srand((unsigned)time(NULL));
auto generate = []()
{
int n = std::rand() % 10 + 110;
Cell cell{n, n};
return cell;
};
std::deque<Cell> deque1(6);
std::generate(deque1.begin(), deque1.end(), generate);
std::cout << "deque1: ";
std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
//2) 以 cont 的内容复制构造底层容器 c 。
std::queue<Cell> queue1(deque1);
while (!queue1.empty())
{
//返回到 queue 中首元素的引用。
//此元素将是调用 pop() 时第一个移除的元素。等效地调用 c.front() 。
//const_reference
std::cout << "queue1.front() before: " << queue1.front() << " ";
//reference
queue1.front() = generate();
std::cout << "after: " << queue1.front();
//从 queue 移除顶元素。等效地调用 c.pop_back()
queue1.pop();
std::cout << std::endl;
}
std::cout << std::endl;
std::queue<Cell> queue2;
int index = 0;
while (index < 6)
{
Cell cell = generate();
if (queue2.size() % 2 == 0)
{
//推给定的元素 value 到 queue 尾。
//1) 等效地调用 c.push_back(value)
queue2.push(cell);
}
else
{
//推给定的元素 value 到 queue 尾。
//2) 等效地调用 c.push_back(std::move(value)),移动语义
queue2.push(std::move(cell));
}
std::cout << "queue2.front() : " << queue2.front() ;
queue2.pop();
std::cout << std::endl;
index++;
}
std::cout << std::endl;
std::queue<Cell> queue3;
index = 0;
while (index < 6)
{
int n = std::rand() % 10 + 110;
//推入新元素到 queue 结尾。原位构造元素,即不进行移动或复制操作。
//以与提供给函数者准确相同的参数调用元素的构造函数。
//等效地调用 c.emplace_back(std::forward<Args>(args)...);
queue3.emplace(n, n);
std::cout << "queue3.front() : " << queue3.front() ;
std::cout << std::endl;
queue3.pop();
index++;
}
std::cout << std::endl;
std::deque<Cell> deque2(6);
std::generate(deque2.begin(), deque2.end(), generate);
std::cout << "deque2: ";
std::copy(deque2.begin(), deque2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::deque<Cell> deque3(6);
std::generate(deque3.begin(), deque3.end(), generate);
std::cout << "deque3: ";
std::copy(deque3.begin(), deque3.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << std::endl;
std::queue<Cell> queue4(deque2);
std::queue<Cell> queue5(deque3);
std::cout << "swap before: " << std::endl;
queuePrint("queue4: ", queue4);
queuePrint("queue5: ", queue5);
//交换容器适配器与 other 的内容。
//等效地调用 using std::swap; swap(c, other.c);
queue4.swap(queue5);
std::cout << "swap after: " << std::endl;
queuePrint("queue4: ", queue4);
queuePrint("queue5: ", queue5);
}
output