名前空間
変種
操作

std::size, std::ssize

From cppreference.com
 
 
イテレータライブラリ
イテレータのコンセプト
イテレータのプリミティブ
アルゴリズムのコンセプトとユーティリティ
間接呼び出し可能コンセプト
共通アルゴリズム要件
(C++20)
(C++20)
(C++20)
ユーティリティ
(C++20)
イテレータアダプタ
Rangeアクセス
(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::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()
{
    // 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

[編集] 関連項目

2つのポインタの差を計算したときに返される符号付き整数型
(typedef) [編集]
sizeof 演算子が返す符号無し整数型
(typedef) [編集]
rangeのサイズと等しい整数を返す
(カスタマイゼーションポイントオブジェクト)[編集]
rangeのサイズと等しい符号付き整数を返す
(カスタマイゼーションポイントオブジェクト)[編集]
English 日本語 中文(简体) 中文(繁體)