|
| 1 | +# cpp_deques.md: Explanation and Usage of Deque (Double-Ended Queue) in STL |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +The `deque` (double-ended queue) is a sequence container in the C++ Standard Library (STL) that allows fast insertion and deletion of elements at both the beginning and the end. Unlike vectors, deques are not guaranteed to store all elements in contiguous memory, but they provide similar functionality with efficient insertions and deletions at both ends. |
| 6 | + |
| 7 | +## Syntax |
| 8 | + |
| 9 | +To use `deque`, you need to include the `<deque>` header: |
| 10 | + |
| 11 | +```cpp |
| 12 | +#include <deque> |
| 13 | +``` |
| 14 | +## Declaration |
| 15 | +You can declare a deque as follows: |
| 16 | + |
| 17 | +```cpp |
| 18 | +std::deque<int> myDeque; |
| 19 | +``` |
| 20 | + |
| 21 | +# Basic Operations |
| 22 | +## Adding Elements |
| 23 | +push_back: Adds an element to the end of the deque. |
| 24 | +push_front: Adds an element to the front of the deque. |
| 25 | + |
| 26 | + |
| 27 | +```cpp |
| 28 | +myDeque.push_back(10); // Adds 10 to the end |
| 29 | +myDeque.push_front(5); // Adds 5 to the front |
| 30 | +``` |
| 31 | + |
| 32 | +# Removing Elements |
| 33 | +pop_back: Removes an element from the end of the deque. |
| 34 | +pop_front: Removes an element from the front of the deque. |
| 35 | + |
| 36 | +```cpp |
| 37 | +myDeque.pop_back(); // Removes the last element |
| 38 | +myDeque.pop_front(); // Removes the first element |
| 39 | +``` |
| 40 | + |
| 41 | +# Accessing Elements |
| 42 | +operator[]: Provides random access to elements. |
| 43 | +at: Provides bounds-checked access to elements. |
| 44 | +front: Accesses the first element. |
| 45 | +back: Accesses the last element. |
| 46 | + |
| 47 | +```cpp |
| 48 | +int firstElement = myDeque.front(); |
| 49 | +int lastElement = myDeque.back(); |
| 50 | +int elementAt2 = myDeque[2]; |
| 51 | +int elementAt3 = myDeque.at(3); // Throws an exception if out of range |
| 52 | +``` |
| 53 | + |
| 54 | +# Iterators |
| 55 | +begin: Returns an iterator to the first element. |
| 56 | +end: Returns an iterator to one past the last element. |
| 57 | + |
| 58 | +```cpp |
| 59 | +for (auto it = myDeque.begin(); it != myDeque.end(); ++it) { |
| 60 | + std::cout << *it << " "; |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +# Example Code |
| 65 | +Here is an example demonstrating the usage of deque: |
| 66 | + |
| 67 | +```cpp |
| 68 | +#include <iostream> |
| 69 | +#include <deque> |
| 70 | + |
| 71 | +int main() { |
| 72 | + std::deque<int> myDeque; |
| 73 | + |
| 74 | + // Adding elements |
| 75 | + myDeque.push_back(10); |
| 76 | + myDeque.push_front(5); |
| 77 | + myDeque.push_back(15); |
| 78 | + myDeque.push_front(1); |
| 79 | + |
| 80 | + // Display elements |
| 81 | + std::cout << "Deque elements: "; |
| 82 | + for (int num : myDeque) { |
| 83 | + std::cout << num << " "; |
| 84 | + } |
| 85 | + std::cout << std::endl; |
| 86 | + |
| 87 | + // Accessing elements |
| 88 | + std::cout << "Front element: " << myDeque.front() << std::endl; |
| 89 | + std::cout << "Back element: " << myDeque.back() << std::endl; |
| 90 | + std::cout << "Element at index 2: " << myDeque[2] << std::endl; |
| 91 | + |
| 92 | + // Removing elements |
| 93 | + myDeque.pop_front(); |
| 94 | + myDeque.pop_back(); |
| 95 | + |
| 96 | + // Display elements after removal |
| 97 | + std::cout << "Deque elements after pop operations: "; |
| 98 | + for (int num : myDeque) { |
| 99 | + std::cout << num << " "; |
| 100 | + } |
| 101 | + std::cout << std::endl; |
| 102 | + |
| 103 | + return 0; |
| 104 | +} |
| 105 | +``` |
| 106 | +# Conclusion |
| 107 | +The deque container in the C++ STL is a versatile sequence container that allows efficient insertion and deletion of elements at both ends. It combines the strengths of both vectors and lists, making it a suitable choice for scenarios where you need quick access to both ends of a sequence. |
0 commit comments