问题描述
在 STL 中,当我执行 s.find("")
时,它返回 0 而 s.find_first_of("")
返回 -1 (npos).这种差异的原因是什么?
In STL, when I do s.find("")
, it returns 0 while s.find_first_of("")
returns -1 (npos). What is the reason for this difference?
推荐答案
s.find(t)
查找子串 t
在 s 中的第一次出现.如果
t
为空,则该事件出现在 s
的开头,并且 s.find(t)
将返回 0.
s.find(t)
finds the first occurrence of the substring t
in s
. If t
is empty, then that occurrence is at the beginning of s
, and s.find(t)
will return 0.
s.find_first_of(t)
查找 t
中一个字符的第一次出现.如果t
为空字符串,则t
中没有字符,所以找不到出现,find_first_of
会返回npos
.
s.find_first_of(t)
finds the first occurrence of one of the characters in t
. If t
is the empty string, then there are no characters in t
, so no occurrence can be found, and find_first_of
will return npos
.
这篇关于搜索空字符串时查找与 find_first_of的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!