C++ 关键词:goto

来自cppreference.com
< cpp‎ | keyword


 
 
C++ 语言
 
 

用法

  • 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 起)