std::optional<T>::end
来自cppreference.com
<tbody>
</tbody>
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());
// 支持范围 for 循环
for (int i : none)
std::println("optional @1 的值为{}", i);
for (int i : some)
std::println("optional @2 的值为 {}", i);
}
输出:
optional @2 的值为 42
参阅
(C++26) |
返回指向起始的迭代器 (公开成员函数) |