Cpp Libs Deque
In C++, `` is part of the Standard Template Library (STL), which 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.
The full name of `` is "double-ended queue", and it exists in C++ as a template class, allowing storage of any type of data.
`` is a dynamic array that provides fast random access capability while allowing efficient insertion and deletion operations at both ends. This makes `` an ideal choice for scenarios that require frequent insertion and deletion of elements.
### Syntax
In C++, using `` requires including the header file `#include `. Here is the basic syntax for ``:
#include #include int main() { std::deque myDeque; // Create an integer deque // You can now perform insertion, deletion, and other operations return 0;}
## Common Operations
Below 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 `deque` container with `n` default-valued elements. |
| `deque(size_type n, const T& value)` | Creates a `deque` container with `n` elements of value `value`. |
| `deque(initializer_list il)` | Constructs a `deque` container using initializer list `il`. |
| `operator=` | Assignment operator, assigns values to the `deque` container. |
| `assign()` | Replaces all elements in the `deque` container with new values. |
| `at(size_type pos)` | Returns the element at position `pos` with bounds checking. |
| `operator[](size_type pos)` | Returns the element at position `pos` 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()` | Removes all elements from the container. |
| `insert(iterator pos, const T& value)` | Inserts `value` at position `pos`. |
| `erase(iterator pos)` | Removes the element at position `pos`. |
| `push_back(const T& value)` | Adds `value` to the end of the container. |
| `pop_back()` | Removes the element at the end of the container. |
| `push_front(const T& value)` | Adds `value` to the front of the container. |
| `pop_front()` | Removes the element at the front of the container. |
| `resize(size_type count)` | Resizes the container to `count`, filling extra positions 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 a simple example of using ``, including inserting, accessing, and removing elements.
## Example
#include
#include
int main(){
std::deque 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<<" ";
}
std::cout<< std::endl;
// Remove 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<<" ";
}
std::cout<< std::endl;
return 0;
}
Output:
Deque contains: 5 10 20 Deque after popping: 10
When you don't know the length of the deque, you can use deque.front() and deque.back() to access the head and tail elements:
## Example
#include
#include
int main(){
std::deque 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;
}
Output:
Front element: 5Back element: 20Modified front element: 15Modified back element: 25
**Note:** Before using front() or back(), make sure 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.
YouTip