【发布时间】:2014-08-02 21:40:39
【问题描述】:
我一直在尝试找到一种在 cmd 中使用字符串输入的方法,并且只要这些字符串存在,就会发生 cmd 的输出。下面是一些可以工作但仍然存在一些问题的代码。
if (text.find("floor") && text.find("knife")!=std::string::npos)
{
std::cout << "Knife and Floor test" << std::endl;
}
if (text.find("knife") !=std::string::npos)
{
std::cout << "just knife" << std::endl;
}
上述方法确实有效。有点儿。输入 floor 并没有给出好的响应。 “地板刀”输出“Kife and Floor test”,这是我想要的。但是只输入“刀”会得到both“刀和地板测试”和“只是刀”。
if (text.find("can") !=std::string::npos)
{
if (text.find("elizabeth") !=std::string::npos)
{
if (text.find("you") !=std::string::npos)
{
std::cout << "1912" << std::endl;
}
}
}
if (text.find("elizabeth") !=std::string::npos)
{
std::cout << "just elizabeth" << std::endl;
}
上述方法同样有效,但也出现了类似的问题。虽然只有“elizabeth”、“can”和“you”存在时“1912”才会输出,但“just elizabeth”也会输出。
最后,如果可能的话,我的目标是能够控制使用运算符输入的内容。如果操作数是错误的方法,我应该使用什么?
目标伪代码
if((("word"||"thisword")&&("this")) !=std::string::npos)
{
run
}
or
if(("thisword") && ("thisotherword") !=std::string::npos)
{
run
}
or
if(("thisword") || ("thisotherword") !=std::string::npos)
{
run
}
有什么想法吗?谢谢!
【问题讨论】:
-
text.find("floor") && text.find("knife")!=std::string::npos这是不对的。您需要将两者分别与 npos 进行比较。