|
| 1 | +--- |
| 2 | +Title: '.shrink_to_fit()' |
| 3 | +Description: 'Requests the deque to reduce its capacity to match its size.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Game Development' |
| 7 | +Tags: |
| 8 | + - 'Classes' |
| 9 | + - 'Containers' |
| 10 | + - 'Deques' |
| 11 | + - 'OOP' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-c-plus-plus' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +In C++, the **`.shrink_to_fit()`** [method](https://www.codecademy.com/resources/docs/cpp/methods) requests the [deque](https://www.codecademy.com/resources/docs/python/deque) to reduce its capacity to match its size. This can help free up unused memory, but does not guarantee that the container will actually shrink. The implementation may choose to ignore this request. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +dequeName.shrink_to_fit(); |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +This method does not take any parameters. |
| 28 | + |
| 29 | +**Return value:** |
| 30 | + |
| 31 | +This method does not return a value, it simply requests that the deque reduce its capacity. |
| 32 | + |
| 33 | +## Example |
| 34 | + |
| 35 | +The example below showcases the use of the `.shrink_to_fit()` method: |
| 36 | + |
| 37 | +```cpp |
| 38 | +#include <iostream> |
| 39 | +#include <deque> |
| 40 | + |
| 41 | +int main() { |
| 42 | + // Create a deque of integers |
| 43 | + std::deque<int> numbers; |
| 44 | + |
| 45 | + // Add elements to the deque |
| 46 | + for (int i = 0; i < 100; ++i) { |
| 47 | + numbers.push_back(i); |
| 48 | + } |
| 49 | + |
| 50 | + std::cout << "Size: " << numbers.size() << std::endl; |
| 51 | + |
| 52 | + // Remove many elements |
| 53 | + while (numbers.size() > 10) { |
| 54 | + numbers.pop_back(); |
| 55 | + } |
| 56 | + |
| 57 | + std::cout << "Size after removal: " << numbers.size() << std::endl; |
| 58 | + |
| 59 | + // Request to shrink capacity |
| 60 | + numbers.shrink_to_fit(); |
| 61 | + |
| 62 | + // Display the elements |
| 63 | + std::cout << "Final deque contents: "; |
| 64 | + for (int num : numbers) { |
| 65 | + std::cout << num << " "; |
| 66 | + } |
| 67 | + |
| 68 | + std::cout << std::endl; |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +The above code generates the following output: |
| 75 | + |
| 76 | +```shell |
| 77 | +Size: 100 |
| 78 | +Size after removal: 10 |
| 79 | +Final deque contents: 0 1 2 3 4 5 6 7 8 9 |
| 80 | +``` |
| 81 | + |
| 82 | +## Codebyte Example |
| 83 | + |
| 84 | +The following codebyte demonstrates the use of the `.shrink_to_fit()` method after removing elements from the deque: |
| 85 | + |
| 86 | +```codebyte/cpp |
| 87 | +#include <iostream> |
| 88 | +#include <deque> |
| 89 | +#include <string> |
| 90 | +
|
| 91 | +int main() { |
| 92 | + std::deque<std::string> myDeque; |
| 93 | +
|
| 94 | + myDeque.push_back("Apple"); |
| 95 | + myDeque.push_back("Banana"); |
| 96 | + myDeque.push_back("Cherry"); |
| 97 | + myDeque.push_back("Date"); |
| 98 | + myDeque.push_back("Elderberry"); |
| 99 | +
|
| 100 | + std::cout << "Original size: " << myDeque.size() << std::endl; |
| 101 | +
|
| 102 | + // Remove some elements |
| 103 | + myDeque.pop_back(); |
| 104 | + myDeque.pop_back(); |
| 105 | + myDeque.pop_back(); |
| 106 | +
|
| 107 | + std::cout << "After removal: " << myDeque.size() << std::endl; |
| 108 | +
|
| 109 | + // Request to shrink capacity |
| 110 | + myDeque.shrink_to_fit(); |
| 111 | +
|
| 112 | + std::cout << "Final contents:"; |
| 113 | + for (const auto& value : myDeque) { |
| 114 | + std::cout << ' ' << value; |
| 115 | + } |
| 116 | + std::cout << std::endl; |
| 117 | +} |
| 118 | +``` |
0 commit comments