Namespaces
Variants

std::inplace_vector<T,N>::inplace_vector

ja.cppreference.net から
 
 
 
 
constexpr inplace_vector() noexcept;
(1) (since C++26)
constexpr explicit inplace_vector( size_type count );
(2) (since C++26)
constexpr inplace_vector( size_type count, const T& value );
(3) (since C++26)
template< class InputIt >
constexpr inplace_vector( InputIt first, InputIt last );
(4) (since C++26)
template< /*container-compatible-range*/<T> R >
constexpr inplace_vector( std::from_range_t, R&& rg );
(5) (since C++26)
constexpr inplace_vector( const inplace_vector& other );
(6) (since C++26)
constexpr inplace_vector( inplace_vector&& other )
    noexcept(N == 0 || std::is_nothrow_move_constructible_v<T>);
(7) (since C++26)
constexpr inplace_vector( std::initializer_list<T> init );
(8) (since C++26)

新しい inplace_vector をさまざまなデータソースから構築します。

1) 空の inplace_vector を構築します。
2) inplace_vector を count 値初期化された 個の要素で構築します。
3) inplace_vector を値 count のコピーを value 個含めて構築します。
4) inplace_vector を範囲 [first, last) の内容で構築します。
5) inplace_vector を範囲 rg の内容で構築します。
/*container-compatible-range*/ の定義については、ranges::to を参照してください。
もし ranges::size(rg) が定数式であり、N よりも大きい場合、プログラムは不適格です。
6) コピーコンストラクタ。 inplace_vector を other の内容のコピーで構築します。
コンストラクタは trivial です (N > 0 と std::is_trivially_copy_constructible_v<T> が両方とも true である場合)。
7) ムーブコンストラクタ。 inplace_vector を other の内容をムーブセマンティクスで構築します。
コンストラクタは trivial です (N > 0 と std::is_trivially_move_constructible_v<T> が両方とも true である場合)。
8) inplace_vector を初期化子リスト init の内容で構築します。

パラメータ

count - コンテナのサイズ
value - コンテナの要素を初期化する値
first, last - コピーする要素のソース範囲を定義するイテレータのペア range
rg - コンテナの要素を初期化する値の範囲
other - コンテナの要素を初期化するためのソースとして使用する別の inplace_vector
init - コンテナの要素を初期化するための初期化子リスト
型要件
-
T はオーバーロード(2,3)を使用するために DefaultInsertable の要件を満たさなければならない

計算量

1) 定数。
2,3) count に対して線形。
4) std:: distance ( first, last ) の距離に対して線形。
5) 線形時間 std :: ranges:: distance ( rg ) の計算量
6,7) サイズに対して線形 other 。
8) サイズは init に対して線形です。

例外

2,3) 以下の場合 std::bad_alloc をスローする: count > N の場合。
変更点: - "Throws" → "をスローする:" - "if" → "以下の場合" - 文末の句読点を日本語に合わせて調整 - HTMLタグ、属性、内のC++コードは完全に保持 - C++専門用語(std::bad_alloc, count, N)は翻訳せず保持
5) std::bad_alloc をスローする( std :: ranges:: size ( rg ) > N の場合)。
8) std::bad_alloc がスローされる条件: init. size ( ) > N の場合。

例

#include <cassert>
#include <initializer_list>
#include <inplace_vector>
#include <new>
#include <print>
#include <ranges>

int main()
{
    std::inplace_vector<int, 4> v1; // overload (1)
    assert(v1.size() == 0 && v1.capacity() == 4);
    
    std::inplace_vector<int, 0> v2; // overload (1), N == 0 is allowed
    assert(v2.size() == 0 && v2.capacity() == 0);
    
    std::inplace_vector<int, 5> v3(3); // overload (2)
    assert(v3.size() == 3 && v3.capacity() == 5);
    std::println("v3 = {}", v3);
    
    try
    {
        std::inplace_vector<int, 3> v(4); // overload (2), throws: count > N
    }
    catch(const std::bad_alloc& ex1)
    {
        std::println("ex1.what(): {}", ex1.what());
    }
    
    std::inplace_vector<int, 5> v4(3, 42); // overload (3)
    assert(v4.size() == 3 && v4.capacity() == 5);
    std::println("v4 = {}", v4);
    
    try
    {
        std::inplace_vector<int, 3> v(4, 2); // overload (3), throws: count > N
    }
    catch(const std::bad_alloc& ex2)
    {
        std::println("ex2.what(): {}", ex2.what());
    }
    
    const auto init = {1, 2, 3};
    
    std::inplace_vector<int, 4> v5(init.begin(), init.end()); // overload (4)
    assert(v5.size() == 3 && v5.capacity() == 4);
    std::println("v5 = {}", v5);
    
    std::inplace_vector<int, 4> v6(std::from_range, init); // overload (5)
    assert(v6.size() == 3 && v6.capacity() == 4);
    std::println("v6 = {}", v6);
    
    std::inplace_vector<int, 4> v7(v6); // overload (6)
    assert(v7.size() == 3 && v7.capacity() == 4);
    std::println("v7 = {}", v7);
    assert(v6.size() == 3);
    
    std::inplace_vector<int, 4> v8(std::move(v6)); // overload (7)
    // Note that after the move v6 is left in valid but indeterminate state.
    assert(v8.size() == 3 && v8.capacity() == 4);
    std::println("v8 = {}", v8);
    
    std::inplace_vector<int, 4> v9(init); // overload (8)
    assert(v9.size() == 3 && v9.capacity() == 4);
    std::println("v9 = {}", v9);
    
    try
    {
        std::inplace_vector<int, 2> v(init); // overload (8), throws: init.size() > N
    }
    catch(const std::bad_alloc& ex3)
    {
        std::println("ex3.what(): {}", ex3.what());
    }
}

出力例:

v3 = [0, 0, 0]
ex1.what(): std::bad_alloc
v4 = [42, 42, 42]
ex2.what(): std::bad_alloc
v5 = [1, 2, 3]
v6 = [1, 2, 3]
v7 = [1, 2, 3]
v8 = [1, 2, 3]
v9 = [1, 2, 3]
ex3.what(): std::bad_alloc

関連項目

コンテナに値を代入する
(公開メンバ関数)
[static]
現在割り当てられているストレージに格納できる要素数を返す
(公開静的メンバ関数)
基となる連続ストレージへの直接アクセス
(公開メンバ関数)
要素数を返す
(公開メンバ関数)
(C++17) (C++20)
コンテナまたは配列のサイズを返す
(関数テンプレート)
(C++17)
基となる配列へのポインタを取得する
(関数テンプレート)