std::inplace_vector<T,N>:: inplace_vector
From cppreference.net
<
cpp
|
container
|
inplace vector
|
constexpr
inplace_vector
(
)
noexcept
;
|
(1) | (C++26以降) |
|
constexpr
explicit
inplace_vector
(
size_type count
)
;
|
(2) | (C++26以降) |
|
constexpr
inplace_vector
(
size_type count,
const
T
&
value
)
;
|
(3) | (C++26以降) |
|
template
<
class
InputIt
>
constexpr inplace_vector ( InputIt first, InputIt last ) ; |
(4) | (C++26以降) |
|
template
<
container-compatible-range
<
T
>
R
>
constexpr inplace_vector ( std:: from_range_t , R && rg ) ; |
(5) | (C++26以降) |
|
constexpr
inplace_vector
(
const
inplace_vector
&
other
)
;
|
(6) | (C++26以降) |
|
constexpr
inplace_vector
(
inplace_vector
&&
other
)
noexcept ( N == 0 || std:: is_nothrow_move_constructible_v < T > ) ; |
(7) | (C++26以降) |
|
constexpr
inplace_vector
(
std::
initializer_list
<
T
>
init
)
;
|
(8) | (C++26以降) |
様々なデータソースから新しい
inplace_vector
を構築します。
1)
空の
inplace_vector
を構築します。このとき
data
(
)
==
nullptr
かつ
size
(
)
==
0
となります。
2)
inplace_vector
を
count
個のデフォルト挿入された要素で構築します。
3)
inplace_vector
を
count
個の
value
値を持つ要素で構築します。
4)
inplace_vector
を範囲
[
first
,
last
)
の内容で構築します。
5)
inplace_vector
を範囲
rg
の内容で構築します。
コンストラクタは、
trivial copy constructor
である。ただし、
N
>
0
かつ
std::
is_trivially_copy_constructible_v
<
T
>
がともに
true
である場合に限る。
コンストラクタは、
trivial move constructor
である。ただし、
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
に対して線形です。
例外
変更点: - "Throws" → "をスローする:" - "if" → "以下の場合" - 文末の句読点を日本語に合わせて調整 - HTMLタグ、属性、
内のC++コードは完全に保持
- C++専門用語(std::bad_alloc, count, N)は翻訳せず保持
例
このコードを実行
#include <cassert> #include <initializer_list> #include <inplace_vector> #include <new> #include <print> #include <ranges> int main() { std::inplace_vector<int, 4> v1; // オーバーロード (1) assert(v1.size() == 0 && v1.capacity() == 4); std::inplace_vector<int, 0> v2; // オーバーロード (1)、N == 0 は許可される assert(v2.size() == 0 && v2.capacity() == 0); std::inplace_vector<int, 5> v3(3); // オーバーロード (2) assert(v3.size() == 3 && v3.capacity() == 5); std::println("v3 = {}", v3); try { std::inplace_vector<int, 3> v(4); // オーバーロード (2)、スロー: count > N } catch(const std::bad_alloc& ex1) { std::println("ex1.what(): {}", ex1.what()); } std::inplace_vector<int, 5> v4(3, 8); // オーバーロード (3) assert(v4.size() == 3 && v4.capacity() == 5); std::println("v4 = {}", v4); try { std::inplace_vector<int, 3> v(4, 2); // オーバーロード (3)、スロー: 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()); // オーバーロード (4) assert(v5.size() == 3 && v5.capacity() == 4); std::println("v5 = {}", v5); std::inplace_vector<int, 4> v6(std::from_range, init); // オーバーロード (5) assert(v6.size() == 3 && v6.capacity() == 4); std::println("v6 = {}", v6); std::inplace_vector<int, 4> v7(v6); // オーバーロード (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)); // オーバーロード (7) // ムーブ後、v6は有効だが不定状態となることに注意。 assert(v8.size() == 3 && v8.capacity() == 4); std::println("v8 = {}", v8); std::inplace_vector<int, 4> v9(init); // オーバーロード (8) assert(v9.size() == 3 && v9.capacity() == 4); std::println("v9 = {}", v9); try { std::inplace_vector<int, 2> v(init); // オーバーロード (8)、スロー: 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)
|
基となる配列へのポインタを取得する
(関数テンプレート) |