std::vector::resize(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’œ 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:

  1. additional default-inserted elements are appended.
  2. 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 πŸ‘‘

  • T must meet the requirements of MoveInsertable and DefaultInsertable in order to use overload (1).
  • T must meet the requirements of CopyInsertable in 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 of T were incorrect. Correct behavior: 1. uses pop_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