名稱空間
變體
操作

std::size, std::ssize

來自 cppreference.com
 
 
迭代器庫
迭代器概念
迭代器原語
演算法概念與工具
間接可呼叫概念
常用演算法要求
(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;
}

[編輯] 註解

特性測試 標準 特性
__cpp_lib_nonmember_container_access 201411L (C++17) std::size()std::datastd::empty
__cpp_lib_ssize 201902L (C++20) std::ssize() (2,4) 和無符號 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) [編輯]
返回等於範圍大小的整數
(自定義點物件)[編輯]
返回等於範圍大小的有符號整數
(自定義點物件)[編輯]