命名空间
变体
操作

std::size, std::ssize

来自 cppreference.cn
< cpp‎ | 迭代器
 
 
迭代器库
迭代器概念
迭代器原语
算法概念和工具
间接可调用概念
通用算法要求
(C++20)
(C++20)
(C++20)
工具
(C++20)
迭代器适配器
范围访问
(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)
 
定义于头文件 <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::sizestd::ranges::ssizestd::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;
}

[编辑] 注释

特性测试 Std 特性
__cpp_lib_nonmember_container_access 201411L (C++17) std::size()std::datastd::empty
__cpp_lib_ssize 201902L (C++20) std::ssize() (2,4) 和 unsigned std::span::size()

[编辑] 示例

#include <cassert>
#include <cstring>
#include <iostream>
#include <vector>
 
int main()
{
    // Works with containers
    std::vector<int> v{3, 1, 4};
    assert(std::size(v) == 3);
 
    // And works with built-in arrays too
    int a[]{-5, 10, 15};
    // Returns the number of elements (not bytes) as opposed to sizeof
    assert(std::size(a) == 3);
    std::cout << "size of a[]: " << sizeof a << '\n'; // 12, if sizeof(int) == 4
 
    // Provides a safe way (compared to sizeof) of getting string buffer size
    const char str[] = "12345";
    // These are fine and give the correct result
    assert(std::size(str) == 6);
    assert(sizeof(str) == 6);
 
    // But use of sizeof here is a common source of bugs
    const char* str_decayed = "12345";
    // std::cout << std::size(str_decayed) << '\n'; // Usefully fails to compile
    std::cout << sizeof(str_decayed) << '\n'; // Prints the size of the pointer!
 
    // Since C++20 the signed size (std::ssize) is available
    auto i = std::ssize(v);
    for (--i; i != -1; --i)
        std::cout << v[i] << (i ? ' ' : '\n');
    assert(i == -1);
 
    // Note that the string literal includes the ending null character, which
    // will be part of the constructed characters array. This makes std::size
    // behave differently from std::strlen and 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) [编辑]
返回一个等于范围大小的整数
(自定义点对象)[编辑]
返回一个等于范围大小的有符号整数
(自定义点对象)[编辑]