std::optional<T>::end
來自 cppreference.com
constexpr iterator end() noexcept; |
(C++26 起) | |
constexpr const_iterator end() const noexcept; |
(C++26 起) | |
返回一個尾後迭代器。等價於 return begin() + has_value();。
目錄 |
[編輯] 引數
(無)
[編輯] 返回值
尾後迭代器
[編輯] 複雜度
常數時間。
[編輯] 注意
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_optional_range_support |
202406L |
(C++26) | 對 std::optional 的範圍支援 |
[編輯] 示例
執行此程式碼
#include <optional> #include <print> int main() { constexpr std::optional<int> none = std::nullopt; // optional @1 constexpr std::optional<int> some = 42; // optional @2 static_assert(none.begin() == none.end()); static_assert(some.begin() != some.end()); // ranged-for loop support for (int i : none) std::println("Optional @1 has a value of {}", i); for (int i : some) std::println("Optional @2 has a value of {}", i); }
輸出
Optional @2 has a value of 42
[編輯] 參閱
(C++26) |
返回指向起始的迭代器 (public member function) |