Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/size"

From cppreference.com
< cpp‎ | iterator
(Notes: +)
(Corrected inconsistent formatting by adding a newline before a '{')
Line 52: Line 52:
 
constexpr auto ssize(const C& c)  
 
constexpr auto ssize(const C& c)  
 
     -> std::common_type_t<std::ptrdiff_t,
 
     -> std::common_type_t<std::ptrdiff_t,
                           std::make_signed_t<decltype(c.size())>> {
+
                           std::make_signed_t<decltype(c.size())>>  
 +
{
 
     using R = std::common_type_t<std::ptrdiff_t,
 
     using R = std::common_type_t<std::ptrdiff_t,
 
                                 std::make_signed_t<decltype(c.size())>>;
 
                                 std::make_signed_t<decltype(c.size())>>;

Revision as of 10:13, 26 November 2019

 
 
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(C++20)
(C++20)
(C++20)
Utilities
(C++20)
Iterator adaptors
Range access
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
sizessize
(C++17)(C++20)
(C++17)
(C++17)
 
Defined in header <iterator>
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)
    -> std::common_type_t<std::ptrdiff_t,

                          std::make_signed_t<decltype(c.size())>>;
(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.

1-2) Returns c.size(), converted to the return type if necessary.
3-4) Returns N.

Contents

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
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;
}

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';
}

Output:

3
3