std::stack::size(3)

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

πŸ’… std::stack::size(3) β€” the C++ Standard Libary manual, section 3, bestie

okay so picture this: you're in the C++ Standard Libary man pages (yes that's how it's spelled on the page, don't come for me 😭) and you land on std::stack::size. That's the one. That's our girl. Let's get into it πŸ’–


✨ NAME ✨

std::stack::size β€” literally just std::stack::size. no notes, the name ate and left no crumbs πŸ‘‘


πŸ¦‹ Synopsis πŸ¦‹

size_type size() const;

So what does it do? It returns the number of elements in the container adaptor. That's it, that's the whole vibe. And it's equivalent to just doing:

return c.size();

no cap, it's giving "middleman who does zero extra work" and honestly? relatable 🫢


🌸 Parameters 🌸

(none)

nothing. nada. zero arguments. she takes no input and she's still iconic πŸ’…


⭐ Return value ⭐

The number of elements in the container adaptor. that's the number you get back. that's the whole gift 🎁


πŸ”₯ Complexity πŸ”₯

Constant. O(1) energy, always. she does not scale with your data, she just knows ✨


πŸŽ€ Example πŸŽ€

here's the little demo, run it and feel something:

// Run this code

#include <algorithm>
#include <cassert>
#include <stack>

int main()
{
    std::stack<int> stack;

    assert(stack.size() == 0);

    const int count = 8;

    for (int i = 0; i != count; ++i)
        stack.push(i);

    assert(stack.size() == count);
}

not the assert(stack.size() == 0) at the start 😭 empty stack, empty heart, verified. then const int count = 8; and we push i from 0 to count, and boom β€” assert(stack.size() == count); passes and we all cheer πŸŽ‰


🌈 See also 🌈

  • empty β€” checks whether the container adaptor is empty (public member function) 🫧
  • size, ssize β€” returns the size of a container or array (C++17) (function template) (C++20) πŸ‘‘

bestie reminder: size() tells you how many elements are in the container adaptor β€” that's the entire job description, and she executes flawlessly every single time πŸ’œ

and that's the page!! sourced straight from http://cppreference.com, dated 2024.06.10, on the std::stack::size(3) page β€” mwah πŸ’‹ go forth and count your elements, bestie πŸ§šπŸ’–

πŸ“¦ where this page comes from