std::size, std::ssize
来自cppreference.com
<tbody>
</tbody>
| 在标头 <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 的自定义重载,从而能检测它。
|
实参依赖查找所找到的 |
(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'; // 12, 若 sizeof(int) == 4
// 提供一种获取字符串缓冲区大小的安全方法(与 sizeof 相比)
const char str[] = "12345";
// 这些方法可以得到正确结果
assert(std::size(str) == 6);
assert(sizeof(str) == 6);
// 而此处使用 sizeof 则是一种常见 BUG 来源
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) | |
(C++20) |
返回等于范围大小的整数 (定制点对象) |
(C++20) |
返回等于范围大小的有符号整数 (定制点对象) |