std::optional<T>::begin
ja.cppreference.net より
constexpr iterator begin() noexcept;
|
(C++26以降) | |
constexpr const_iterator begin() const noexcept;
|
(C++26以降) | |
*this が値を保持している場合、その保持されている値を指すイテレータを返します。そうでない場合、終端イテレータの値を返します。
戻り値
has_value ( ) が true の場合、格納されている値へのイテレータ。それ以外の場合、末尾後イテレータ。
計算量
定数。
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_optional_range_support
|
202406L
|
(C++26) |
std::optionalの
レンジサポート
|
例
このコードを実行
#include <optional> #include <print> #include <vector> int main() { constexpr std::optional<int> none{std::nullopt}; constexpr std::optional<int> some{42}; static_assert(none.begin() == none.end()); static_assert(some.begin() != some.end()); // 範囲ベースforループのサポート for (int i : none) std::println("'none' has a value of {}", i); for (int i : some) std::println("'some' has a value of {}", i); std::optional<std::vector<int>> many({0, 1, 2}); for (const auto& v : many) std::println("'many' has a value of {}", v); }
出力:
'some' has a value of 42 'many' has a value of [0, 1, 2]
関連項目
|
(C++26)
|
終端を指すイテレータを返す
(公開メンバ関数) |