The C++ standard library
In C++, is part of the Standard Template Library (STL); it provides an implementation of a double-ended queue.
A double-ended queue is a linear data structure that allows insertion and deletion operations at both ends.
's full name is 'double-ended queue'; it exists in C++ as a template class, allowing storage of any type of data.
is a dynamic array that provides fast random access while allowing efficient insertion and deletion at both ends. This makes an ideal choice for scenarios requiring frequent insertion and deletion of elements.
Syntax
In C++, to use , you need to include the header #include . The following are 's basic syntax:
#include
#include
int main() {
std::deque myDeque; // 创建一个整数类型的双端队列
// Next, insertion, deletion, and other operations can be performed
return 0;
}
common operations
Here are some commonly used member functions of the std::deque container:
| Function name | Description |
|---|---|
deque() |
Default constructor; creates an empty deque container. |
deque(size_type n) |
Creates a n with deque container. |
deque(size_type n, const T& value) |
Creates a n default-valued elements, or a value 's deque container. |
deque(initializer_list |
using an initializer list il to construct deque container. |
operator= |
Assignment operator; assigns to deque container. |
assign() |
Replaces deque all elements in the container with new values. |
at(size_type pos) |
Return pos element at position, with bounds checking. |
operator[](size_type pos) |
Return pos element at position, without bounds checking. |
front() |
Returns a reference to the first element. |
back() |
Returns a reference to the last element. |
begin() |
returns an iterator pointing to the first element. |
end() |
Returns an iterator pointing to the position after the last element. |
rbegin() |
Returns a reverse iterator pointing to the last element. |
rend() |
Returns a reverse iterator pointing to the position before the first element. |
empty() |
Checks whether the container is empty. |
size() |
Returns the number of elements in the container. |
max_size() |
Returns the maximum number of elements the container can hold. |
clear() |
Clears all elements in the container. |
insert(iterator pos, const T& value) |
in pos Inserts at position value . |
erase(iterator pos) |
Removes the pos element at position. |
push_back(const T& value) |
Adds at the end of the container value . |
pop_back() |
Removes the element at the end of the container. |
push_front(const T& value) |
Adds at the front of the container value . |
pop_front() |
Removes the element at the front of the container. |
resize(size_type count) |
Resizes the container to count, padding the excess with default values. |
swap(deque& other) |
Swaps the contents of two deque containers. |
get_allocator() |
Returns a copy of the allocator object used to construct the deque. |
Example
Below is an example using is a simple example, including element insertion, access, and deletion operations.
Example
#include
#include
int
main
(
)
{
std
::
deque
<
int
myDeque;
// Insert elements
myDeque.
push_back
(
10
)
;
myDeque.
push_back
(
20
)
;
myDeque.
push_front
(
5
)
;
// Access elements
std
::
cout
<<
"Deque contains: "
;
for
(
int
i
=
0
;
i
<
myDeque.
size
(
)
;
++
i
)
{
std
::
cout
<<
myDeque
[
i
]
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
// Delete elements
myDeque.
pop_back
(
)
;
myDeque.
pop_front
(
)
;
// Access elements again
std
::
cout
<<
"Deque after popping: "
;
for
(
int
i
=
0
;
i
<
myDeque.
size
(
)
;
++
i
)
{
std
::
cout
<<
myDeque
[
i
]
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
return
0
;
}
Output result:
Deque contains: 5 10 20
Deque after popping: 10
When you don't know the deque's length, you can use deque.front() and deque.back() to access the head and tail elements:
Example
#include
#include
int
main
(
)
{
std
::
deque
<
int
d;
// Add elements to the deque
d.
push_back
(
10
)
;
d.
push_back
(
20
)
;
d.
push_front
(
5
)
;
// Access the front element
std
::
cout
<<
"Front element: "
<<
d.
front
(
)
<<
std
::
endl
;
// Access the back element
std
::
cout
<<
"Back element: "
<<
d.
back
(
)
<<
std
::
endl
;
// Modify the front element
d.
front
(
)
=
15
;
// Modify the back element
d.
back
(
)
=
25
;
// Access elements again
std
::
cout
<<
"Modified front element: "
<<
d.
front
(
)
<<
std
::
endl
;
std
::
cout
<<
"Modified back element: "
<<
d.
back
(
)
<<
std
::
endl
;
return
0
;
}
The output is:
Front element: 5
Back element: 20
Modified front element: 15
Modified back element: 25
**Note:** Before using front() or back(), ensure the deque is not empty; otherwise it will cause undefined behavior. To check whether the deque is empty, you can use the empty() member function.