【问题标题】:C++ find strings in input and use operatorsC++ 在输入中查找字符串并使用运算符
【发布时间】: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") &amp;&amp; text.find("knife")!=std::string::npos 这是不对的。您需要将两者分别与 npos 进行比较。

标签: c++ string operators


【解决方案1】:

试试下面的

    if ( text.find( "floor" ) != std::string::npos && 
         text.find( "knife" ) != std::string::npos )
    {
        std::cout << "Knife and Floor test" << std::endl;
    }
    else if ( text.find( "knife" ) != std::string::npos )
    {
        std::cout << "just knife" << std::endl;
    }

//...

        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;
                }
            }
        }
        else if ( text.find( "elizabeth" ) != std::string::npos )
        {
            std::cout << "just elizabeth" << std::endl;
        }


if( ( text.find( "this" ) != std:;string::npos &&
      ( text.find("word" ) != std::string::npos  ||
        text.find( "thisword" )!= std::string::npos ) )

{

      run

}

if ( text.find( "thisword" ) != std::string::npos && 
     text.find( "thisotherword" ) != std::string::npos )
{

      run

}

if ( text.find( "thisword" ) != std::string::npos || 
     text.find( "thisotherword" ) !=std::string::npos )
{

      run

}

第一个if语句可以改写如下方式

if ( text.find( "knife" ) != std::string::npos )
{
    if ( text.find( "floor" ) != std::string::npos ) 
    {
        std::cout << "Knife and Floor test" << std::endl;
    }
    else 
    {
        std::cout << "just knife" << std::endl;
    }
}

【讨论】:

  • 你的第一个例子正是我想要的!讨厌它是如此简单!谢谢!
  • 太棒了。多次使用字符串时是否需要牢记“if else”,例如使用刀的示例?
【解决方案2】:

只输入“刀”会给出“刀和地板测试”和“只是 刀。”

嗯,这是什么意思:

if (text.find("floor") && text.find("knife")!=std::string::npos)
{
    std::cout << "Knife and Floor test" << std::endl;
}

实际上是什么意思?这意味着如果搜索“knife”没有返回string::npos,并且搜索“floor”返回某些结果为真(例如,string::npos),然后打印“刀和地板测试”。这就是 Vlad 对您的代码所做的更改正常工作的原因。

至于这个:

虽然“1912”在只有“elizabeth”“can”和“you”时才输出 现在,“just elizabeth”的输出也是如此。

粘贴else if 代替if 的原因是因为您的代码实际上正在处理此部分:

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;
}

作为独立测试。也就是说,如果没有 else,上段是否评估为 true 并执行它自己的事情并不重要,因为下段将评估 无论上面发生什么。插入else 表示“仅当上述关联的if 没有评估为真时才测试此条件语句”。

我希望这可以帮助您理解为什么建议的解决方案是正确的。

【讨论】:

  • 至于为什么事情不起作用,就像它为什么起作用一样提供信息。谢谢!将屏幕打印笔记。
【解决方案3】:

您可以创建一些辅助函数,以便更轻松地测试字符串是否匹配字符串列表中的某一项 (find_any()),或匹配列表中的所有项目 (find_all())。

    if (find_all(text1, "knife", "floor")) {
        std::cout << "knife and floor test pass 1\n";
    }

    if (!find_all(text2, "knife", "floor")) {
        std::cout << "knife and floor test pass 2\n";
    }

    if (!find_all(text3, "knife", "floor")) {
        std::cout << "knife and floor test pass 3\n";
    }

    if (find_any(text2, "knife", "floor")) {
        if (findx(text2, "knife")) {
            std::cout << "knife or floor found knife\n";
        }
    }

    if (find_any(text3, "knife", "floor")) {
        if (findx(text3, "floor")) {
            std::cout << "knife or floor found floor\n";
        }
    }

findx() 是一个简单的辅助函数,可为您提供布尔结果:

bool findx (std::string haystack, std::string s) {
    return haystack.find(s) != haystack.npos;
}

find_all()find_any() 可以用可变参数模板函数实现,但也有其他方法:

enum FIND_X { FIND_ALL, FIND_ANY };

template <typename... Strings>
bool find_any (std::string haystack, Strings... args) {
    return find_x<FIND_ANY, Strings...>(haystack, args...);
}

template <typename... Strings>
bool find_all (std::string haystack, Strings... args) {
    return find_x<FIND_ALL, Strings...>(haystack, args...);
}

template <FIND_X, typename T>
bool find_x (std::string haystack, T s) {
    return findx(haystack, s);
}

template <FIND_X OP, typename T, typename... Strings>
bool find_x (std::string haystack, T s, Strings... args) {
    if (haystack.find(s) != haystack.npos) {
        if (OP == FIND_ANY) return true;
    } else {
        if (OP == FIND_ALL) return false;
    }
    return find_x<OP, Strings...>(haystack, args...);
}

A live demo.

【讨论】:

  • 有趣。使事情变得更简单,更精确。我可以使用很多方法。我将不得不尝试并找到真正利用这一切的方法。大帮助!谢谢!
猜你喜欢
  • 1970-01-01
  • 2016-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-03
  • 2020-11-07
  • 1970-01-01
相关资源
最近更新 更多