std::optional<T>::end
来自 cppreference.cn
constexpr iterator end() noexcept; |
(自 C++26 起) | |
constexpr const_iterator end() const noexcept; |
(自 C++26 起) | |
返回一个过尾迭代器。等价于 return begin() + has_value();。
目录 |
[编辑] 参数
(无)
[编辑] 返回值
过尾迭代器
[编辑] 复杂度
常数。
[编辑] 注释
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__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) |
返回指向开头的迭代器 (公共成员函数) |