Namespaces
Variants

std:: size, std:: ssize

From cppreference.net
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)
size ssize
(C++17) (C++20)
(C++17)
(C++17)
定义于头文件 <array>
定义于头文件 <deque>
定义于头文件 <flat_map>
定义于头文件 <flat_set>
定义于头文件 <forward_list>
定义于头文件 <inplace_vector>
定义于头文件 <iterator>
定义于头文件 <list>
定义于头文件 <map>
定义于头文件 <regex>
定义于头文件 <set>
定义于头文件 <span>
定义于头文件 <string>
定义于头文件 <string_view>
定义于头文件 <unordered_map>
定义于头文件 <unordered_set>
定义于头文件 <vector>
template < class C >
constexpr auto size ( const C & c ) - > decltype ( c. size ( ) ) ;
(1) (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) (C++20 起)
template < class T, std:: size_t N >
constexpr std:: size_t size ( const T ( & array ) [ N ] ) noexcept ;
(3) (C++17 起)
template < class T, std:: ptrdiff_t N >
constexpr std:: ptrdiff_t ssize ( const T ( & array ) [ N ] ) noexcept ;
(4) (C++20 起)

返回给定范围的大小。

1,2) 返回 c. size ( ) ,如有必要会转换为返回类型。
3,4) 返回 N

目录

参数

c - 具有 size 成员函数的容器或视图
array - 任意类型的数组

返回值

1) c. size ( )
2) static_cast < std:: common_type_t < std:: ptrdiff_t ,
std:: make_signed_t < decltype ( c. size ( ) ) >>> ( c. size ( ) )
3,4) N

异常

1,2) 可能抛出由实现定义的异常。

重载

可以为那些未提供合适的 size() 成员函数但仍可被检测的类和枚举类型提供自定义的 size 重载。

通过 实参依赖查找 找到的 size 重载可用于自定义 std :: ranges:: size std :: ranges:: ssize std :: ranges:: empty 的行为。

(C++20 起)

可能的实现

size (1)
template<class C>
constexpr auto size(const C& c) -> decltype(c.size())
{
    return c.size();
}
ssize (2)
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());
}
size (3)
template<class T, std::size_t N>
constexpr std::size_t size(const T (&array)[N]) noexcept
{
    return N;
}
ssize (4)
template<class T, std::ptrdiff_t N>
constexpr std::ptrdiff_t ssize(const T (&array)[N]) noexcept
{
    return N;
}

注释

功能测试 标准 功能特性
__cpp_lib_nonmember_container_access 201411L (C++17) std::size() , std::data std::empty
__cpp_lib_ssize 201902L (C++20) std::ssize() ( 2,4 ) 和无符号 std::span::size()

示例

#include <cassert>
#include <cstring>
#include <iostream>
#include <vector>
int main()
{
    // 适用于容器
    std::vector<int> v{3, 1, 4};
    assert(std::size(v) == 3);
    // 同样适用于内置数组
    int a[]{-5, 10, 15};
    // 返回元素数量(而非字节数),与 sizeof 不同
    assert(std::size(a) == 3);
    std::cout << "size of a[]: " << sizeof a << '\n'; // 若 sizeof(int) == 4 则输出 12
    // 提供获取字符串缓冲区大小的安全方式(与 sizeof 相比)
    const char str[] = "12345";
    // 以下代码正确运行并给出正确结果
    assert(std::size(str) == 6);
    assert(sizeof(str) == 6);
    // 但在此处使用 sizeof 是常见的错误来源
    const char* str_decayed = "12345";
    // std::cout << std::size(str_decayed) << '\n'; // 编译失败(这是有用的)
    std::cout << sizeof(str_decayed) << '\n'; // 输出指针的大小!
    // 自 C++20 起可使用有符号尺寸(std::ssize)
    auto i = std::ssize(v);
    for (--i; i != -1; --i)
        std::cout << v[i] << (i ? ' ' : '\n');
    assert(i == -1);
    // 注意字符串字面量包含结束空字符,
    // 这将作为构造字符数组的一部分。这使得 std::size
    // 的行为与 std::strlen 和 std::string::size 不同:
    constexpr char symbols[] = "0123456789";
    static_assert(std::size(symbols) == 11);
    static_assert(std::string(symbols).size() == 10);
    assert(std::strlen(symbols) == 10);
}

可能的输出:

size of a[]: 12
8
4 1 3

参见

两个指针相减时返回的有符号整数类型
(typedef)
sizeof 运算符返回的无符号整数类型
(typedef)
返回等于范围大小的整数
(定制点对象)
返回等于范围大小的有符号整数
(定制点对象)