C++11 如何在std::字符串中搜索特定关键字
我试图在std::string中搜索关键字并返回一个布尔值,如下所示C++11 如何在std::字符串中搜索特定关键字,c++11,C++11,我试图在std::string中搜索关键字并返回一个布尔值,如下所示 std::string select_class() { std::string Class; //Defines the string 'Class' std::string user_input; //Defines the string 'input //Tells the user available commands std::cout << "Class selec
std::string select_class()
{
std::string Class; //Defines the string 'Class'
std::string user_input; //Defines the string 'input
//Tells the user available commands
std::cout << "Class selection - Available classes =
\n\nArcher\nRouge\nWarrior\nMage\n\nCommands: \nset_class - selects your
class for this game";
std::cout << "\ninfo (class_name) - provides information regarding the
specific class";
while (true)
{
std::cin >> user_input;//Asks user to input a command
if (std::find(user_input,"set_class") == true)//Tests to see if
//specific keyword has been inputted
{
std::cout << "Please enter your class:";
}
}
}
我希望能够使while循环中的if语句返回布尔值或具有等效结果
很抱歉格式不好,因为这是我的第一篇文章 使用string.find,如下所示:
std::cin >> user_input;//Asks user to input a command
if (user_input.find("set_class") != std::string::npos)//Tests to see if
//specific keyword has been inputted
{
std::cout << "Please enter your class:";
}
你查过std::find返回的是什么吗?请查看此文档:@John3136是的,但我不太明白如何让它工作。@AlexHodges如果你知道它返回迭代器,为什么要将它与布尔值进行比较?