What does string::npos mean in this code?

后端 未结 12 1265
傲寒
傲寒 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:10

    npos is just a token value that tells you that find() did not find anything (probably -1 or something like that). find() checks for the first occurence of the parameter, and returns the index at which the parameter begins. For Example,

      string name = "asad.txt";
      int i = name.find(".txt");
      //i holds the value 4 now, that's the index at which ".txt" starts
      if (i==string::npos) //if ".txt" was NOT found - in this case it was, so  this condition is false
        name.append(".txt");
    

提交回复
热议问题