std::deque::resize(3)

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

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

  1. additional default-inserted elements are appended.
  2. additional copies of value are 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 🧚

  • T must meet the requirements of MoveInsertable and DefaultInsertable in order to use overload (1). them's the rules fr fr.
  • T must 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() passed value by value β†’ correct behavior: passes by const reference.
  • 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. T was not required to be MoveInsertable β†’ correct behavior: 1. uses pop_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