c++ - 在返回值的函数中使用内部循环返回

标签 c++

<分区>

我怀疑在这种情况下,C++ 中的 return 是如何工作的。一旦条件匹配并且我们需要返回值,循环会发生什么。

some function(){
    for( )//outer loop with i and other values
    {
        for( )// inner loop with i and other values {
            some condition using if which on match return a value val (call it x here)
            return x;
        }
    }
    return -1
}

最佳答案

循环没有任何“发生”。就像往常一样,return 从函数返回。

循环消失了,就像 return 语句后面的任何代码一样。

唯一“发生”的事情是您的自动存储持续时间对象(粗略地说,您的局部变量)将被自动销毁。

关于c++ - 在返回值的函数中使用内部循环返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37274962/

相关文章:

c++ - 当您的外部 SDK 只有.dll、.lib、.h 文件时如何链接和构建项目

c++ - : How to make threads write to private arrays and merge all the arrays once all the threads finished processing 并行 omp

c++ - Qt 执行外部程序

c++ - 从带有 opengl 代码的 C++ 转换为带有 GUI 的 QT 应用程序?

C++ - 无法从队列中删除元素

c++ - 我怎样才能将具有点的 vector 更改为具有其值的 vector

c++ - LNK2019 - 未解析的外部符号 C++

c++ - 重置二维 vector 数组

c++ - clang-format 过度缩进概念

c++ - 为什么我的程序会在拥有线程的情况下生成 LdrpLoaderLock 死锁?