std::vector::size(3)
β¨ 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms