The C++ std::deque::resize() function is used to adjust the size of the deque. If the new size is greater than the current size, the deque is expanded and the new elements are added to the end. Similarly, if the new size is smaller, the deque is truncated, and the excess elements at the end are removed.
This function has 2 polymorphic variants: with using the default and the value version (you can find the syntaxes of all the variants below).
SyntaxFollowing is the syntax for std::deque::resize() function.
void resize (size_type n); or void resize (size_type n, const value_type& val);Parameters
This function does not return anything.
ExceptionsIf reallocation fails then bad_alloc exception is thrown.
Time complexityThe time complexity of this function is Linear i.e. O(n)
ExampleIn the following example, we are going to consider the basic usage of the resize() function.
#include <iostream> #include <deque> int main() { std::deque<int> a; a.resize(4); for (auto& elem : a) { std::cout << elem << " "; } std::cout << std::endl; return 0; }Output
Output of the above code is as follows −
0 0 0 0Example
Consider the following example, where we are going to resize the deque and assign the value.
#include <iostream> #include <deque> int main() { std::deque<char> x; x.resize(3, 'A'); for (auto& elem : x) { std::cout << elem << " "; } std::cout << std::endl; return 0; }Output
Following is the output of the above code −
A A AExample
Let's look at the following example, where we are going to decreasing the size of the deque using resize() function.
#include <iostream> #include <deque> int main() { std::deque<int> a{1,12,23,34,45,56}; a.resize(4); for (auto& elem : a) { std::cout << elem << " "; } std::cout << std::endl; return 0; }Output
If we run the above code it will generate the following output −
1 12 23 34
deque.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4