What does string::npos mean in this code?

后端 未结 12 1267
傲寒
傲寒 2020-11-30 18:50

What does the phrase std::string::npos mean in the following snippet of code?

found = str.find(str2);

if (found != std::string::npos)
    std::         


        
12条回答
  •  无人及你
    2020-11-30 19:18

    An answer for these days of C++17, when we have std::optional:

    If you squint a bit and pretend std::string::find() returns an std::optional (which it sort of should...) - then the condition becomes:

    auto position = str.find(str2);
    
    if ( position.has_value() ) {
        std::cout << "first 'needle' found at: " << found.value() << std::endl;
    }
    

提交回复
热议问题