std::set::size(3)

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

πŸ’– NAME πŸ’–

Welcome to std::set::size(3), straight from the C++ Standard Libary β€” yes, that’s the spelling on the page, and I’m keeping it exactly as-is, no cap πŸ’…

std::set::size β€” okay so the NAME line literally says std::set::size and then std::set::size again. That’s the name, bestie, no plot twist πŸŽ€


πŸ¦‹ Synopsis πŸ¦‹

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

Returns the number of elements in the container, i.e. std::distance(begin(), end()). Since C++11 it’s noexcept, periodt ✨


🌸 Parameters 🌸

(none) β€” literally zero parameters. not a single argument. the void of parameters. πŸ•³οΈβœ¨


πŸŽ€ Return value πŸŽ€

The number of elements in the container. That’s it, bestie β€” the return value is giving β€œcount of elements” and nothing else πŸ’–


⭐ Complexity ⭐

Constant. No cap, it’s constant complexity, and that’s the whole vibe 🌈


πŸ“ Example πŸ“

The following code uses size to display the number of elements in a std::set<int>:

// Run this code

#include <iostream>
#include <set>

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

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

Output:

nums contains 4 elements.

πŸ’œ See also πŸ’œ

The original table is a lil scrambled, but every piece is right here, iykyk:

  • empty β€” checks whether the container is empty (public member function)
  • max_size β€” returns the maximum possible number of elements (public member function)
  • size β€” (C++17) returns the size of a container or array (function template)
  • ssize β€” (C++20) returns the size of a container or array (function template)

mwah πŸ’‹ source/date from the footer: http://cppreference.com, 2024.06.10 β€” catch you in the next one, bestie 🫢

πŸ“¦ where this page comes from