std::size, std::ssize
| Defined in header <array>
|
||
| Defined in header <deque>
|
||
| Defined in header <forward_list>
|
||
| Defined in header <iterator>
|
||
| Defined in header <list>
|
||
| Defined in header <map>
|
||
| Defined in header <regex>
|
||
| Defined in header <set>
|
||
| Defined in header <span>
|
(since C++20) |
|
| Defined in header <string>
|
||
| Defined in header <string_view>
|
||
| Defined in header <unordered_map>
|
||
| Defined in header <unordered_set>
|
||
| Defined in header <vector>
|
||
| template <class C> constexpr auto size(const C& c) -> decltype(c.size()); |
(1) | (since C++17) |
| template <class C> constexpr auto ssize(const C& c) |
(2) | (since C++20) |
| template <class T, std::size_t N> constexpr std::size_t size(const T (&array)[N]) noexcept; |
(3) | (since C++17) |
| template <class T, std::ptrdiff_t N> constexpr std::ptrdiff_t ssize(const T (&array)[N]) noexcept; |
(4) | (since C++20) |
Returns the size of the given container c or array array.
c.size(), converted to the return type if necessary.N.Contents |
[edit] Parameters
| c | - | a container with a size member function
|
| array | - | an array of arbitrary type |
[edit] Return value
The size of c or array
[edit] Possible implementation
| First version |
|---|
template <class C> constexpr auto size(const C& c) -> decltype(c.size()) { return c.size(); } |
| Second version |
template <class C> constexpr auto ssize(const C& c) -> std::common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>> { using R = std::common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>>; return static_cast<R>(c.size()); } |
| Third version |
template <class T, std::size_t N> constexpr std::size_t size(const T (&array)[N]) noexcept { return N; } |
| Fourth version |
template <class T, std::ptrdiff_t N> constexpr std::ptrdiff_t ssize(const T (&array)[N]) noexcept { return N; } |
[edit] Example
#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'; // since C++20 the signed size (ssize) can avail auto i = std::ssize(v); for (--i; i != -1; --i) { std::cout << v[i] << ' '; } std::cout << "\n" "i = " << i << '\n'; }
Output:
3 3 4 1 3 i = -1
[edit] See also
| signed integer type returned when subtracting two pointers (typedef) | |
| unsigned integer type returned by the sizeof operator (typedef) |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
