std::forward_list<T,Allocator>:: resize
From cppreference.net
<
cpp
|
container
|
forward list
|
void
resize
(
size_type count
)
;
|
(1) |
(C++11以降)
(C++26以降 constexpr) |
|
void
resize
(
size_type count,
const
value_type
&
value
)
;
|
(2) |
(C++11以降)
(C++26以降 constexpr) |
コンテナを count 個の要素を含むようにサイズ変更します:
- count が現在のサイズと等しい場合、何も行いません。
- 現在のサイズが count より大きい場合、コンテナは最初の count 要素に縮小されます。
- 現在のサイズが count より小さい場合、以下の処理が行われます:
1)
追加の
default-inserted
要素が末尾に追加されます。
2)
追加の
value
のコピーが追加されます。
目次 |
パラメータ
| count | - | コンテナの新しいサイズ |
| value | - | 新しい要素を初期化する値 |
| 型要件 | ||
|
-
|
||
計算量
現在のサイズと count の差に対して線形。最初の消去要素/挿入終了位置に到達するためのリスト走査により、追加の複雑さが生じる可能性があります。
注記
オーバーロード
(
1
)
における値初期化が望ましくない場合、例えば要素が非クラス型でありゼロ初期化が不要な場合、
カスタム
Allocator::construct
を提供することで回避できます。
例
このコードを実行
#include <forward_list> #include <iostream> void print(auto rem, const std::forward_list<int>& c) { for (std::cout << rem; const int el : c) std::cout << el << ' '; std::cout << '\n'; } int main() { std::forward_list<int> c = {1, 2, 3}; print("The forward_list holds: ", c); c.resize(5); print("After resize up to 5: ", c); c.resize(2); print("After resize down to 2: ", c); c.resize(6, 4); print("After resize up to 6 (initializer = 4): ", c); }
出力:
The forward_list holds: 1 2 3 After resize up to 5: 1 2 3 0 0 After resize down to 2: 1 2 After resize up to 6 (initializer = 4): 1 2 4 4 4 4
関連項目
|
格納可能な最大要素数を返す
(public member function) |
|
|
コンテナが空かどうかをチェックする
(public member function) |