std::deque::resize(3)
π std::deque::resize(3) π
okay bestie, so this is a page from the C++ Standard Library manual, and today's main character is std::deque::resize β the glow-up/down-slash for your deque β¨
πΈ NAME πΈ
std::deque::resize β bestie, it's literally std::deque::resize. that's the whole name. iconic of it tbh π
β¨ SYNOPSIS β¨
void resize( size_type count ); (1)
void resize( size_type count, const value_type& value ); (2)
So here's the tea: it resizes the container so it contains count elements β and if count == size() it literally does nothing. no cap, zero effort, we love a low-maintenance function π«Ά
If the current size is greater than count, the container gets reduced to its first count elements (bye bestie π).
If the current size is less than count, then:
- additional default-inserted elements are appended.
- additional copies of
valueare appended.
π PARAMETERS π
countβ the new size of the container, i.e. how many elements we're fitting in this fit π¦valueβ the value used to initialize the new elements with π
π§ TYPE REQUIREMENTS π§
Tmust meet the requirements of MoveInsertable and DefaultInsertable in order to use overload (1). them's the rules fr fr.Tmust meet the requirements of CopyInsertable in order to use overload (2).
β RETURN VALUE β
(none) β it gives you nothing back. not a crumb. but honestly? the real reward was the elements we resized along the way π
π₯ COMPLEXITY π₯
Linear in the difference between the current size and count. so the more you resize, the more work it does β plan accordingly bestie π
π NOTES π
If value-initialization in overload (1) is undesirable β for example, if the elements are of non-class type and zeroing out is not needed β it can be avoided by providing a custom Allocator::construct. fancy custom behavior, we stan a bespoke allocator π
πΈ EXAMPLE πΈ
// Run this code
#include <deque>
#include <iostream>
void print(auto rem, const std::deque<int>& c)
{
for (std::cout << rem; const int el : c)
std::cout << el << ' ';
std::cout << '\n';
}
int main()
{
std::deque<int> c = {1, 2, 3};
print("The deque holds: ", c);
c.resize(5);
print("After resize up to 5: ", c);
c.resize(2);
print("After resize down to 2: ", c);
c.resize(6, 4);
print("After resize up to 6 (initializer = 4): ", c);
}
π¦ OUTPUT π¦
The deque holds: 1 2 3
After resize up to 5: 1 2 3 0 0
After resize down to 2: 1 2
After resize up to 6 (initializer = 4): 1 2 4 4 4 4
look at those zeros showing up out of nowhere β default insertion really said "i'm the main character now" πβ¨
π DEFECT REPORTS π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. so like, the standard got a patch and we're all just living in the fixed timeline π«Ά
- LWG 679 β applied to C++98 β behavior as published:
resize()passedvalueby value β correct behavior: passes byconstreference. - LWG 1418 β applied to C++98 β behavior as published: the behavior of
resize(size())was not specified β correct behavior: specified. - LWG 2033 β applied to C++11 β behavior as published: 1. elements were removed by using
erase(); 2.Twas not required to be MoveInsertable β correct behavior: 1. usespop_back(); 2. required.
π SEE ALSO π
sizeβ returns the number of elements (public member function) πinsertβ inserts elements (public member function) βeraseβ erases elements (public member function) β
okay that's the whole page bestie, you're now officially a std::deque::resize scholar ππ this was pulled from http://cppreference.com, dated 2024.06.10 β so it's giving vintage-but-still-slaying, and it's filed under std::deque::resize(3). mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms