字符串.输入返回true始终(C )
String.find returns true always (C++)
im试图让boolean找到的dust_word返回true,如果它找到单词/字符,如果没有找到词,但是无论我在文本中写了什么,它始终返回true。循环本身有效,已经尝试过。包括iostream和字符串。
while(timestorun){
found_word = text.find("khgdawjugfdjhawbdjkhsadgawkdsa");
if(found_word){
cout << "FOUND!!!" << endl;
}
else if(!found_word){
cout << "Found problem!!!!!"<< endl;
}
timestorun--;
}
有什么建议?
您应该与npos
进行比较。find
不返回布尔值。
found_word = text.find("khgdawjugfdjhawbdjkhsadgawkdsa") != std::string::npos;
0
(即 false
)才能在索引0
找到子字符串时返回。
另外,您的第二个条件是多余的 - 如果found_word
是false
,我个人保证!found_word
将为true
。
应该更像这样:
int main ()
{
int found = text.find("some text");
if (found != std::string::npos)
{
//do stuff if word is there
}
else
{
//do stuff when word isnt there
}
}
text.find如果单词不在那里,则应返回-1,否则它将返回字符串中的位置
相关文章:
- 无论条件是否为true,if总是在c++中执行
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 禁止显示由于常量为零而比较始终为假的警告
- 如何使变量从 x 到 y,然后从 y 返回到 x 并始终重复该过程
- Arduino-C++ bool 不会从 false 变为 true
- `如果constexpr(false)`始终评估为true
- 为什么当 std::sort 中的比较函数始终返回 true 时出现运行时错误
- "if"条件的计算结果始终为 "true"
- 字符串.输入返回true始终(C )
- JSON CPP ISMEMBER()始终返回true
- C++ - 函数始终返回 true
- c++ MySql 连接器查询始终返回 true,即使出错也是如此
- if(!buydat.empty()) 验证失败(始终为 true)
- Qt 编译错误:endl 将始终计算为 true
- binary_search始终返回 true C++
- Leap Year Basic C++Boolean始终返回true
- Address将始终计算为true error
- 警告:函数将始终返回True