std::optional<T>::end
从 zh.cppreference.net
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}; // 可选对象 @1 constexpr std::optional<int> some{42}; // 可选对象 @2 static_assert(none.begin() == none.end()); static_assert(some.begin() != some.end()); // 支持范围 for 循环 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)
|
返回指向起始位置的迭代器
(公开成员函数) |