std::vector::resize(3)
π std::vector::resize β the glow-up/shrink-up button for your vector (3)
okay so bestie, this is the C++ Standard Library page for std::vector::resize, and it's honestly one of the most-used little functions in the whole vector family. it changes how many elements your container holds. that's it. that's the vibe. β¨
πΈ NAME πΈ
std::vector::resize β literally std::vector::resize, no notes, the name ate and left no crumbs π
β¨ SYNOPSIS β¨
void resize( size_type count ); (1)
(constexpr since C++20)
void resize( size_type count, const value_type& value ); (2)
(constexpr since C++20)
so you've got two overloads, and both got that (constexpr since C++20) glow-up. π«Ά
π¦ DESCRIPTION π¦
Resizes the container so it contains count elements. if count == size(), it does absolutely nothing β no cap, it just vibes. π
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 value are appended.
π PARAMETERS π
countβ the new size of the container, aka how many elements you want it to hold πvalueβ the value used to initialize the new elements with
π TYPE REQUIREMENTS π
Tmust meet the requirements ofMoveInsertableandDefaultInsertablein order to use overload (1).Tmust meet the requirements ofCopyInsertablein order to use overload (2).
fr fr, check these before you ship β the compiler will absolutely not be gentle about it π
β RETURN VALUE β
(none) β it gives you nothing back, it's giving "I did the work, don't ask for a receipt" π
π₯ COMPLEXITY π₯
Linear in the difference between the current size and count. There's additional complexity possible due to reallocation if capacity is less than count β so if your vector has to grow its actual storage, that's extra work on top. π
π EXCEPTIONS π
If an exception is thrown for any reason, these functions have no effect β that's the strong exception safety guarantee, and she is iconic for it. π
Although not explicitly specified, std::length_error is thrown if the capacity required by the new vector would exceed max_size().
Now brace yourself, because in overload (1): if T's move constructor is not noexcept and T is not CopyInsertable into *this, vector will use the throwing move constructor (since C++11). If it throws, the guarantee is waived and the effects are unspecified. ππ that's the scary part, bestie β no safety net.
π§ NOTES π§
If value-initialization in overload (1) is undesirable β say, the elements are of non-class type and zeroing out is not needed β it can be avoided by providing a custom Allocator::construct.
Vector capacity is never reduced when resizing to smaller size, because that would invalidate all iterators, rather than only the ones that would be invalidated by the equivalent sequence of pop_back() calls. the way that's actually a thoughtful design choiceβ¦ obsessed. π
π EXAMPLE π
go ahead, run this code:
#include <vector>
#include <iostream>
void print(auto rem, const std::vector<int>& c)
{
for (std::cout << rem; const int el : c)
std::cout << el << ' ';
std::cout << '\n';
}
int main()
{
std::vector<int> c = {1, 2, 3};
print("The vector 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 vector 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
see how the 0 0 appeared out of nowhere? that's the default-insertion doing its thing, and then 4 4 4 4 is the copy overload showing off. ate. π₯
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards β so like, the old behavior was the old behavior, but the committee came back and fixed it. β¨
- LWG 679 β applied to C++98. As published:
resize()passed value by value. Correct behavior: passes by const reference. - LWG 1525 β applied to C++98. As published: the behavior of
resize(size())was not specified. Correct behavior: specified. - LWG 2033 β applied to C++11. As published: 1. elements were removed by using
erase(); 2. the type requirements ofTwere incorrect. Correct behavior: 1. usespop_back(); 2. corrected. - LWG 2066 β applied to C++11. As published: overload (1) did not have the exception safety guarantee of overload (2). Correct behavior: added.
π SEE ALSO π
sizeβ returns the number of elements (public member function)insertβ inserts elements (public member function)eraseβ erases elements (public member function)
ngl the resize(size()) thing being "not specified" before LWG 1525 is such a "we'll figure it out later" moment π they did figure it out later. slay.
that's the whole page, bestie β go resize something responsibly π
source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::vector::resize(3) β mwah, see u in the next manpage ππ¦π
π¦ 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