std::vector::size(3)

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

✨ std::vector::size(3) ✨

Bestie, welcome to the <vector> chapter of the C++ Standard Library, where we're about to have a whole moment over std::vector::size. Like, this function is THAT girl β€” she tells you exactly how many elements are in your container, no cap. πŸŽ€


🌸 Synopsis 🌸

size_type size() const;  (noexcept since C++11)
                         (constexpr since C++20)

Okay so what does she actually do? She returns the number of elements in the container, which is literally std::distance(begin(), end()). That's the vibe check for your vector's population. πŸ’…


πŸ¦‹ Parameters πŸ¦‹

(none) β€” she takes nothing, she gives everything. Periodt. ✨


πŸ’– Return value πŸ’–

The number of elements in the container. That's it. That's the whole tea. 🍡


πŸ”₯ Complexity πŸ”₯

Constant. Like, O(1) constant. She's fast, she's efficient, she did not come to play. ⚑


🌈 Example 🌈

Not me about to show you the cutest little code block where size displays the number of elements in a std::vector<int>:

// Run this code

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> nums {1, 3, 5, 7};

    std::cout << "nums contains " << nums.size() << " elements.\n";
}

Output:

nums contains 4 elements.

Slay. Look at her go. πŸ‘‘


πŸ‘‘ See also πŸ‘‘

  • capacity β€” returns the number of elements that can be held in currently allocated storage (public member function) 🫢
  • empty β€” checks whether the container is empty (public member function) πŸ’€
  • max_size β€” returns the maximum possible number of elements (public member function) 🌟
  • resize β€” changes the number of elements stored (public member function) 🧚
  • ssize (C++17, C++20) β€” returns the size of a container or array (function template) 🌸

ngl bestie, size is lowkey the main character of vector member functions. She's giving "I know exactly how many elements are in this container" energy and honestly? We're obsessed. πŸ’œ

Source: cppreference.com Β· Version: 2024.06.10 Β· Section: std::vector::size(3)

Mwah πŸ’‹ go forth and count your elements, queen. πŸ¦‹βœ¨

πŸ“¦ where this page comes from