Defined in header <iterator> | ||
---|---|---|
template < class C > constexpr auto size( const C& c ) -> decltype(c.size()); | (1) | (since C++17) |
template < class T, size_t N > constexpr size_t size( const T (&array)[N] ) noexcept; | (2) | (since C++17) |
Returns the size of the given container c
or array array
.
c.size()
.N
.c | - | a container with a size method |
array | - | an array of arbitrary type |
The size of c
or array
.
noexcept
specification: noexcept
In addition to being included in <iterator>
, std::size
is guaranteed to become available if any of the following headers are included: <array>
, <deque>
, <forward_list>
, <list>
, <map>
, <regex>
, <set>
, <string>
, <unordered_map>
, <unordered_set>
, and <vector>
.
First version |
---|
template <class C> constexpr auto size(const C& c) -> decltype(c.size()) { return c.size(); } |
Second version |
template <class T, std::size_t N> constexpr std::size_t size(const T (&array)[N]) noexcept { return N; } |
#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:
3 3
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/iterator/size