C++ deque resize() Function
Example
Change the size of a deque to 2 elements:
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars.resize(2);
for (string car : cars) {
cout << car << "\n";
}
Definition and Usage
The resize() function changes the number of elements in a deque.
If the new size is smaller, elements are removed from the end. If the new size is larger, new elements are added at the end.
The resize function is defined in the <deque> header file.
Syntax
deque.resize(size_t size);
deque.resize(size_t size, <type> value);
The size_t data type is a non-negative integer. <type> refers to the type of the data that the deque contains.
Parameter Values
| Parameter | Description |
|---|---|
| size | Required. The new number of elements. |
| value | Optional. The value of the new elements, if the deque gets bigger. If you leave it out, the new elements get a default value, like 0 for numbers and an empty string for strings. |
Technical Details
| Returns: | Nothing (void). |
|---|
Related Pages
Read more about deques in our Deque Tutorial.