C++ C++;如果字符串中有字符,则为else

C++ C++;如果字符串中有字符,则为else,c++,if-statement,C++,If Statement,标题不是很有用,但基本上我要做的是对一个txt文件进行检查,找到包含我要查找的内容的单词 下面的代码正确地完成了它,并且完全符合我的要求。但是 void qu() { for (Word word : word2) { string uq = word.getWord(); if (uq.find("qa") != std::string::npos) { cout << uq <<

标题不是很有用,但基本上我要做的是对一个txt文件进行检查,找到包含我要查找的内容的单词

下面的代码正确地完成了它,并且完全符合我的要求。但是

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';
        }
    }
}
void qu()
{
for(Word:word2)
{
字符串uq=word.getWord();
if(uq.find(“qa”)!=std::string::npos)
{

cout下面的代码将按照与您相同的顺序执行,并且稍微整洁一些:

for (Word word : word2)
{
    string uq = word.getWord();
    string lookup = "qa";
    for ( char c: "abcdefghijklmnopqrstuvwxyz")
    {
        lookup[1] = c;
        if (uq.find(lookup) != std::string::npos)
        {
            cout << uq << '\n';
            break;
        }
    }
}
for(Word:word2)
{
字符串uq=word.getWord();
字符串查找=“qa”;
用于(字符c:“abcdefghijklmnopqrstuvwxyz”)
{
查找[1]=c;
if(uq.find(lookup)!=std::string::npos)
{

不能用数据替换代码

创建一个包含要查找的单词的容器,例如
std::set
。然后添加一个内部循环,对每个单词执行
if
检查

以下是一个例子:

std::set<std::string> const search_words =  { "qa", "qb", "qc", /*...*/ "qz" };
for (auto const& word : word2)
{
    std::string const uq = word.getWord();
    for (auto const& search_word : search_words)
    {
        if (uq.find(search_word) != std::string::npos)
        {
            std::cout << uq << '\n';
        }
    }
}
std::set const search_words={“qa”、“qb”、“qc”、/*…*/“qz”};
for(自动常量和word:word2)
{
std::string const uq=word.getWord();
for(自动常量和搜索词:搜索词)
{
if(uq.find(search_word)!=std::string::npos)
{

std::cout由互联网用户回答

您可以将所有字符串放入一个向量中,并使用for循环迭代和查找每个字符串:

void qu()
{
for (Word word : word2)
{
    std::string uq = word.getWord();
    std::vector<std::string> strings_to_find =
    {
        "qa", "qb", "qc", "qd", "qe", "qf", "qg", "qh", "qi", "qj", "qk",
        "ql", "qm", "qn", "qo", "qp", "qr", "qs", "qt", "qv", "qw", "qx",
        "qy", "qz"
    }; // no "qu" in original code

    for (auto& str : strings_to_find)
    {
        if (uq.find(str) != std::string::npos)
        {
            cout << uq << '\n';
        }
    }
}
}
void qu()
{
for(Word:word2)
{
std::string uq=word.getWord();
std::要查找的向量字符串=
{
“qa”、“qb”、“qc”、“qd”、“qe”、“qf”、“qg”、“qh”、“qi”、“qj”、“qk”,
“ql”、“qm”、“qn”、“qo”、“qp”、“qr”、“qs”、“qt”、“qv”、“qw”、“qx”,
“qy”,“qz”
};//原始代码中没有“qu”
用于(自动&str:strings\u-to\u-find)
{
如果(uq.find(str)!=std::string::npos)
{

如果某个答案确实回答了您的问题,请将其标记为已回答。不要将答案复制到您的问题中!堆栈溢出不是这样工作的……如果代码工作,则更像是一个有自己站点的情况。不要只转储代码;解释问题并解释解决方案。