制作一个函数,该函数确定两个字符串是否相等,但忽略它们的大小写。到目前为止,我有
bool isEqual(string str1, string str2) {
bool result = true;
for(int i = 0; I < str1.length(); i++) {
if(str1[i]==str2[i]||str1[i]==toupper(str2[i])||str1[i]==tolower(str2[i])){
//keep result==true
}
else {
result = false;
}
}
return result;
}
但是,这似乎是解决该问题逻辑的一种非常低效的方法,有人有任何建议吗?谢谢
答案 0 :(得分:0)
我的建议。
grep -oP '(?<="company":")[^"]*' * | awk 'BEGIN{FS=":";OFS=","} prev!=$1 && val{print prev":"val;val=""} {val=(val?val OFS:"")$2;prev=$1} END{if(val){print prev":"val}}'
或std::tolower()
。std::toupper()
一个更简单的版本是比较字符串开头的长度,如果长度不相等则返回bool isEqual(string str1, string str2)
{
auto iter1 = begin(str1);
auto end1 = end(str1);
auto iter2 = begin(str2);
auto end2 = end(str2);
for ( ; iter1 != end1 && iter2 != end2; ++iter1, ++iter2 )
{
// This will also work.
// if ( std::tolower(*iter1) != std::tolower(*iter2) )
if ( std::toupper(*iter1) != std::toupper(*iter2) )
{
return false;
}
}
// We come here only if we have compared all the characters
// of at least one of the strings.
// The two strings are equal only if we have compared all the
// characters of BOTH the strings.
return (iter1 == end1 && iter2 == end2);
}
。感谢@PeteBecker的建议。
false