名稱空間
變體
操作

std::span<T,Extent>::size

來自 cppreference.com
< cpp‎ | 容器‎ | span
constexpr size_type size() const noexcept;
(C++20 起)

返回 span 中的元素數量。

目錄

[編輯] 引數

(無)

[編輯] 返回值

span 中的元素數量。

[編輯] 注意

特性測試 標準 特性
__cpp_lib_ssize 201902L (C++20) std::ssize 和無符號 std::span::size

[編輯] 示例

#include <iostream>
#include <span>
 
void show_sizes(std::span<const int> span)
{
    std::cout
        << span                 .size() << ' ' // 8
        << span.first(7)        .size() << ' ' // 7
        << span.first<6>()      .size() << ' ' // 6
        << span.last(5)         .size() << ' ' // 5
        << span.last<4>()       .size() << ' ' // 4
        << span.subspan(2, 3)   .size() << ' ' // 3
        << span.subspan<3, 2>() .size() << ' ' // 2
        << '\n';
}
 
int main()
{
    int antique_array[]{1, 2, 3, 4, 5, 6, 7, 8};
    show_sizes(antique_array);
}

輸出

8 7 6 5 4 3 2

[編輯] 參閱

構造一個 span
(public member function) [編輯]
返回序列的位元組大小
(public member function) [編輯]