我的算法不是很强。函数有两个版本,如果传递的字符串中有大写字母则返回。
First 不检查每次迭代的条件。第二个在第一次出现大写字母后结束。
总体上哪个更快?
bool isAnyCapLetters(const std::string &s) {
int flag = 0;
for (auto el : s)
flag += isupper(el);
return static_cast<bool>(flag);
}
bool isAnyCapLetters(const std::string &s) {
for (auto el : s)
if (isupper(el)) return true;
return false;
}
最佳答案
第二个提前返回并且对于以大写字母开头的长字符串会更快。
关于c++ - 哪个版本的 "find an occurence"函数更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22687477/