標題不是很豐富,但基本上我試圖做的是對txt文件運行檢查,並找到包含我正在尋找的單詞。C++ else if字符串中的字符
下面的代碼正確地完成了它,並且完全如何我想要它來做到這一點。但!
void qu()
{
for (Word word : word2)
{
string uq = word.getWord();
if (uq.find("qa") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qb")!= std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qc") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qd") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qe") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qf") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qg") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qh") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qi") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qj") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qk") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("ql") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qm") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qn") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qo") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qp") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qq") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qr") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qs") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qt") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qv") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qw") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qx") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qy") != std::string::npos)
{
cout << uq << '\n';
}
else if (uq.find("qz") != std::string::npos)
{
cout << uq << '\n';
}
}
}
我想以更整潔的方式做到這一點。
如果有人能指出我正確的方向,如cplusplus引用鏈接或其他文件將是很棒的。
如果答案實際上已經回答了你的問題,那麼請將其標記爲已回答。不要將答案複製到你的問題中!這不是Stack Overflow的工作原理...... –
如果代碼起作用,它更像是一個[Code Review](https://codereview.stackexchange.com/),它有自己的站點。 –