C++ 关键词:goto
来自cppreference.com
用法
- goto 语句:用作语句的声明
示例
运行此代码
#include <cassert>
#include <string>
[[nodiscard]] auto get_first_line(const std::string& string)
{
std::string first_line {};
for (const auto character : string)
switch (character)
{
case '\n':
goto past_for; // 打断 '范围 for 循环'
default:
first_line += character;
break;
}
past_for:
return first_line;
}
int main()
{
assert(get_first_line("Hello\nworld!") == "Hello");
}
参阅
|
(C++17 起) |
|
(C++23 起) |
| (C++20 起) |