C++ Standard Library<deque>
In C++,<deque>is part of the Standard Template Library (STL), providing an implementation of a double-ended queue.
A deque is a linear data structure that allows insertion and deletion operations at both ends.
<deque>Its full name is "double-ended queue", and in C++ it exists in the form of a template class, allowing storage of any type of data.
<deque>It is a dynamic array that provides fast random access capabilities while allowing efficient insertion and deletion operations at both ends. This makes<deque>it an ideal choice for scenarios that require frequent insertion and deletion of elements.
Syntax
In C++, use<deque>Need to include header file#include <deque>. The following is<deque>Basic syntax:
#include <iostream>
#include <deque>
int main() {
std::deque<int> myDeque; // 创建一个整数类型的双端队列
// 接下来可以进行插入、删除等操作
return 0;
}
Common operations
Below are some common member functions of the std::deque container:
| Function name | Function Description |
|---|---|
deque() | the default constructor to create an emptydequeContainer. |
deque(size_type n) | Create a container containingnelements with default valuesdequeContainer. |
deque(size_type n, const T& value) | Create a container containingna value isvalueofdequeContainer. |
deque(initializer_list<T> il) | Using an initializer listilConstructordequeContainer. |
operator= | assignment operator to assign todequeContainer. |
assign() | Replace with a new valuedequeall elements in the container. |
at(size_type pos) | returnposelement at position, with range checking. |
operator[](size_type pos) | returnposelement at position, without range checking. |
front() | Returns a reference to the first element. |
back() | Returns a reference to the last element. |
begin() | Returns an iterator 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() | Removes all elements from the container. |
insert(iterator pos, const T& value) | InposInsert at positionvalueElement. |
erase(iterator pos) | Removeposelement at position. |
push_back(const T& value) | Add to the end of the containervalueElement. |
pop_back() | Removes the element at the end of the container. |
push_front(const T& value) | Add to the front of the containervalueElement. |
pop_front() | Removes the element at the front of the container. |
resize(size_type count) | Resize the container tocountextra parts are filled with default values. |
swap(deque& other) | Swap twodequecontents of the container. |
get_allocator() | Returns a copy of the allocator object used to construct the deque. |
Example
Below is an example using<deque>A simple example, including insertion, access, and deletion operations of elements.
Example
#include <deque>
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:
Deque contains: 5 10 20 Deque after popping: 10
When the length of the deque is unknown, deque.front() and deque.back() can be used to access the front and back elements:
Example
#include <deque>
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 that the deque is not empty; otherwise, it will cause undefined behavior. If you need to check whether the deque is empty, you can use the empty() member function.
other extensions