std::size,std::ssize (3) Linux Manual Page
std::size,std::ssize – std::size,std::ssize
Synopsis
Defined in header<iterator>
template <class C>
(1)(since C++ 17)
constexpr auto size(const C &c) -> decltype(c.size());
template <class C>
constexpr auto ssize(const C &c)(2)(since C++ 20)
-> std::common_type_t<std::ptrdiff_t,
std::make_signed_t<decltype(c.size())>>;
template <class T, std::size_t N>
(3)(since C++ 17)
constexpr std::size_t size(const T (&array)[N]) noexcept;
template <class T, std::ptrdiff_t N>
(4)(since C++ 20)
constexpr std::ptrdiff_t ssize(const T (&array)[N]) noexcept;
Returns the size of the given container c or array array.
1-2) Returns c.size(), converted to the return type if necessary.
3-4) Returns N.
Parameters
c – a container with a size method
array – an array of arbitrary type
Return value
The size of c or array
Notes
In addition to being included in <iterator>, std::size and std::ssize are guaranteed to become available if any of the following headers are included: <array>, <deque>, <forward_list>, <list>, <map>, <regex>, <set>
, <span>
(since C++20), <string>, <string_view>, <unordered_map>, <unordered_set>, and <vector>.
Possible implementation
First version
Second version
Third version
Fourth version
Example
// Run this code
#include <iostream>
#include <vector>
#include <iterator>
int main()
{
std::vector<int> v = {3, 1, 4};
std::cout << std::size(v) << '\n';
int a[] = {-5, 10, 15};
std::cout << std::size(a) << '\n';
}
Output:
