Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 返回函数后是否需要休息?_C++_Oop - Fatal编程技术网

C++ 返回函数后是否需要休息?

C++ 返回函数后是否需要休息?,c++,oop,C++,Oop,这是我的代码中的一个函数: bool same_community(string s1, string s2)//check if student 1 & student 2 are in the same community { for(int i=0;i<number_of_communities;i++) if(community[i].contains(s1) && community[i].contains(s2)) {

这是我的代码中的一个函数:

bool same_community(string s1, string s2)//check if student 1 & student 2 are in the same community
{
for(int i=0;i<number_of_communities;i++)    
    if(community[i].contains(s1) && community[i].contains(s2))
    {
        return true;
        break;
    }
return false
}
bool same_社区(字符串s1,字符串s2)//检查学生1和学生2是否在同一社区中
{

对于(int i=0;i否,返回语句足以退出循环。

否。它是死代码,很可能会被编译器的优化器删除。请删除它,因为它会降低代码的可读性。

否。不需要中断。返回就足够了。

否,您不需要。返回表达式将执行n flow离开函数。返回后的任何内容都不会执行。

否您不需要使用
中断
。它用于提前终止循环,但
返回
(在子例程中)也将终止其内部的任何循环。任何跟随
return
的都是死代码。

不,但我建议更好的缩进:-)啊…我甚至建议始终使用{}即使是单行程序。如果将s1和s2作为
const string&
传递,返回后也不必中断。根据编译器(设置)的不同,永远不会达到中断您也会收到警告或错误。如果您没有收到警告,这表明您应该使用提供更多警告的设置进行编译,或者在必要时更改编译器:-)请注意,如果break是可访问的,则意味着将达到return false,因此您最初的return true将毫无用处。此外,这意味着return语句在方法中是no op。