close
Sometimes , we may want to find word in string.
we can use string::find to achieve this purpose.
int main()
{
string str = "This is a book";
string word = "book";
int found = str.find(word);
if(found !=string::nops)
{
cout<<"Congratulation!! we find the word , book."<<endl;
}
else
{
cout<<"This word is not exist!!"<<endl;
}
return 0;
}
string::nops的定義如下:
static const size_t nops = -1;
一般來說 , nops昰定義為 -1 , 代表的是greatest possible value .
但因為size_t昰 unsigned integral type , 所以若定義為size_t , 那麼他會return largest representative value.
string::find()
if we find the word in the string , find() will return the position in string of the word or find() will return nops.
全站熱搜
留言列表