Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/size"

From cppreference.com
< cpp‎ | iterator
m (Added Spanish link, s/method/member function)
(+ ranges)
Line 36: Line 36:
 
{{dcl end}}
 
{{dcl end}}
  
Returns the size of the given container {{tt|c}} or array {{tt|array}}.
+
Returns the size of the given range.
  
 
@1-2@ Returns {{tt|c.size()}}, converted to the return type if necessary.
 
@1-2@ Returns {{tt|c.size()}}, converted to the return type if necessary.
Line 43: Line 43:
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | c | a container with a {{tt|size}} member function}}
+
{{par | c | a container or view with a {{tt|size}} member function}}
 
{{par | array | an array of arbitrary type}}
 
{{par | array | an array of arbitrary type}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
The size of {{tt|c}} or {{tt|array}}
+
The size of {{tt|c}} or {{tt|array}}.
 +
 
 +
===Exceptions===
 +
@1-2@ {{cpp/impldef exception item}}
 +
 
 +
===Overloads===
 +
Custom overloads of {{tt|size}} may be provided for classes and enumerations that do not expose a suitable {{tt|size()}} member function, yet can be detected.
 +
 
 +
{{rrev|since=c++20|
 +
Overloads of {{tt|size}} found by [[cpp/language/adl|argument dependent lookup]] can be used to customize the behavior of {{c|std::ranges::size}}, {{c|std::ranges::ssize}}, and {{c|std::ranges::empty}}.
 +
}}
  
 
===Possible implementation===
 
===Possible implementation===
Line 116: Line 126:
 
{{dsc inc | cpp/types/dsc ptrdiff_t}}
 
{{dsc inc | cpp/types/dsc ptrdiff_t}}
 
{{dsc inc | cpp/types/dsc size_t}}
 
{{dsc inc | cpp/types/dsc size_t}}
 +
{{dsc inc | cpp/ranges/dsc size}}
 +
{{dsc inc | cpp/ranges/dsc ssize}}
 
{{dsc end}}
 
{{dsc end}}
  
 
+
{{langlinks|es|ja|ru|zh}}
{{langlinks|es|ja|zh}}
+

Revision as of 00:53, 4 April 2021

 
 
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 <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)
    -> 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 range.

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

Contents

Parameters

c - a container or view with a size member function
array - an array of arbitrary type

Return value

The size of c or array.

Exceptions

1-2) May throw implementation-defined exceptions.

Overloads

Custom overloads of size may be provided for classes and enumerations that do not expose a suitable size() member function, yet can be detected.

Overloads of size found by argument dependent lookup can be used to customize the behavior of std::ranges::size, std::ranges::ssize, and std::ranges::empty.

(since C++20)

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

See also

signed integer type returned when subtracting two pointers
(typedef) [edit]
unsigned integer type returned by the sizeof operator
(typedef) [edit]
returns an integer equal to the size of a range
(customization point object)[edit]
returns a signed integer equal to the size of a range
(customization point object)[edit]