![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Deque::resize() Function
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).
Syntax
Following is the syntax for std::deque::resize() function.
void resize (size_type n); or void resize (size_type n, const value_type& val);
Parameters
- n − It indicates the size of the new container.
- val − It indicates the initial value for container elements.
Return value
This function does not return anything.
Exceptions
If reallocation fails then bad_alloc exception is thrown.
Time complexity
The time complexity of this function is Linear i.e. O(n)
Example
In 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 0
Example
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 A
Example
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