【发布时间】:2014-11-05 03:52:03
【问题描述】:
什么意思
在包含返回的循环之后未能提供返回是错误
在“C++ 入门第五章”,第 295 页?
尤其是编译器没有检测到这个错误,运行时发生的事情是不确定的。
我使用的书样如:(vs2013)
#include "stdafx.h"
#include "iostream"
#include "string"
using std::string;
bool str_subrange(const string &str1, const string&str2) {
if (str1.size() == str2.size())
return str1 == str2;
auto size = (str1.size() < str2.size()) ? str1.size() : str2.size();
for (decltype(size)i = 0; i != size; ++i) {
if (str1[i] != str2[i])
return false;
else
return true;
}
}
int _tmain(int argc, _TCHAR* argv[]) {
if (str_subrange("lyc", "talent"))
std::cout << "yes" << std::endl;
else
std::cout << "nope" << std::endl;;
system("pause");
return 0;
}
效果很好,我想知道哪些情况是必须“返回”的。
【问题讨论】:
-
尝试拨打
str_subrange("", "anything")。 -
请注意,您的函数仅将
str[0]与str[1]进行比较 - 在第一次迭代中它返回。
标签: c++