c++ - Asymmetry of string::find_first_of and string::find_last_of -
with fellow member function string::find_first_of in c++ standard library can search in empty substring:
s.find_first_of(c, s.size())
or
s.find_first_of(c, string::npos)
but cannot search in empty substring string::find_last_of
; next phone call search in substring containing (only) first character:
s.find_last_of(c, 0)
i think imperfection of c++ standard library, isn't it?
i don't see asymmetry here. in fact quite opposite, appears symmetrical. think of find_first_of
search right starting position, while find_last_of
search left starting position.
the name find_last_of
has misleading quality it: implies natural forward search, except homecoming lastly occurrence instead of first one. however, bidirectional sequences 1 can ignore "forward" nature of name , think of of backward search. backward search returns first occurrence, proceeds to left starting point. point of view, function symmetrical find_first_of
.
edit: after reading comments understand point. so, problem current semantics of pos
parameter makes impossible specify empty search part find_last_of
. yes, makes sense. agree, indeed can seen inconsistency in find_last_of
design.
for consistency purposes, expect find_last_of
non-inclusive respect pos
value. in case specification of target position of xpos
returned find_last_of
be
xpos < pos , xpos < size();
in case s.find_last_of(c, 0)
search empty prefix, while s.find_last_of(c, s.size())
search entire string.
however standard says
xpos <= pos , xpos < size();
i don't know why decided give pos
parameter such inclusive meaning. thought create easier understand.
c++
No comments:
Post a Comment