void reserve(size_type n); // (1) C++26
概要
指定した要素数を格納できるよう容量を確保する。
効果
n <= capacity()である場合、何もしない。そうでなければ、予約ブロックを確保することでcapacity()を増加させる。
戻り値
なし
事後条件
capacity() >= nである。
例外
事後条件を満たすことでcapacity()がmax_size()を超える場合、length_error例外を送出する。また、アロケータが送出するあらゆる例外を送出する。
計算量
確保される予約ブロックの数に対して線形時間
備考
*thisの要素を指す全ての参照・ポインタ・イテレータ、および終端イテレータは有効なまま保たれる。
例
#include <hive>
#include <print>
int main()
{
std::hive<int> h;
// 100要素分の領域を予約する
h.reserve(100);
std::println("capacity >= 100 : {}", h.capacity() >= 100);
// 予約済みの領域に要素を挿入する
int* p = &*h.insert(42);
// 予約した容量の範囲内では、挿入時に新たなメモリ確保が発生しない
for (int i = 0; i < 50; ++i) {
h.insert(i);
}
std::println("{}", *p);
}
出力
capacity >= 100 : true
42
バージョン
言語
- C++26
処理系
- Clang: 22 ❌
- GCC: 16.1 ❌
- Visual C++: 2026 Update 2 ❌
関連項目
参照
- P0447R28 Introduction of
std::hiveto the standard library- C++26で
hiveが追加された
- C++26で
- LWG Issue 4379.
hive::reserve()needs Throws: element adjusted to match block min/max considerations- C++26で、
capacity()がmax_size()を超える場合にlength_errorを送出するよう例外指定が明確化された
- C++26で、