deque resize() function in C++ STL
The deque::resize() is an inbuilt function in C++ STL which changes the size of the deque.
If the given size is greater than the current size, then new elements are inserted at the end of the deque.
If the given size is smaller than the current size, then extra elements are destroyed.
Syntax:
deque_name.resize(n)
Parameter: The function accepts only one mandatory parameter n which specifies the size of the deque.
Return value: The function does not return anything.
Below program illustrates the above function:
Program 1:
// C++ program to illustrate the // deque::resize() function #include <bits/stdc++.h> using namespace std; int main() { deque< int > dq = { 10, 20, 30, 40, 50 }; cout << "Size before resize " << dq.size() << "\n" ; // Prints the deque elements cout << "The contents of deque :" ; for ( auto it = dq.begin(); it != dq.end(); ++it) cout << *it << " " ; cout << endl; // resize to 7 dq.resize(7); // // Prints the deque elements after resize() cout << "Size after resize " << dq.size() << "\n" ; cout << "The contents of deque :" ; for ( auto it = dq.begin(); it != dq.end(); ++it) cout << *it << " " ; return 0; } |
Size before resize 5 The contents of deque :10 20 30 40 50 Size after resize 7 The contents of deque :10 20 30 40 50 0 0
Program 2:
// C++ program to illustrate the // deque::resize() function #include <bits/stdc++.h> using namespace std; int main() { deque< int > dq = { 10, 20, 30, 40, 50 }; cout << "Size before resize " << dq.size() << "\n" ; // Prints the deque elements cout << "The contents of deque :" ; for ( auto it = dq.begin(); it != dq.end(); ++it) cout << *it << " " ; cout << endl; // resize to 3 dq.resize(3); cout << "Size after resize " << dq.size() << "\n" ; cout << "The contents of deque :" ; for ( auto it = dq.begin(); it != dq.end(); ++it) cout << *it << " " ; return 0; } |
Size before resize 5 The contents of deque :10 20 30 40 50 Size after resize 3 The contents of deque :10 20 30
Time Complexity: O(N)
Recommended Posts:
- valarray resize() function in C++
- forward_list resize() function in C++ STL
- list resize() function in C++ STL
- deque max_size() function in C++ STL
- deque insert() function in C++ STL
- deque rbegin() function in C++ STL
- deque rend() function in C++ STL
- deque assign() function in C++ STL
- deque::front() and deque::back() in C++ STL
- deque::emplace_front() and deque::emplace_back() in C++ STL
- deque::empty() and deque::size() in C++ STL
- deque::pop_front() and deque::pop_back() in C++ STL
- deque::clear() and deque::erase() in C++ STL
- deque::begin() and deque::end in C++ STL
- deque::at() and deque::swap() in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.