Find if a string contains a character in C++ (boost allowed)

后端 未结 7 755
遇见更好的自我
遇见更好的自我 2020-12-31 05:58

Suppose I have a string and I want to find whether a specific character (like \'|\') is present or not, what is the best and fastest technique to do so? I know string find i

7条回答
  •  时光说笑
    2020-12-31 06:36

    Use std::string::find

    if (str.find('|') != std::string::npos)
    {
        // ...
    }
    

    There's unlikely to be anything more efficient. O(n) is the best you can do. The standard library implementation should be pretty much optimal.

提交回复
热议问题