On 9/21/2010 11:14 PM, Pete Becker wrote:
> On 2010-09-21 14:11:54 -0400, Stefan van Kessel said:
>
>> On 9/21/2010 8:03 PM, Pete Becker wrote:
>>> On 2010-09-21 13:52:04 -0400, Paul said:
>>>
>>>> I have a path in a string ie: std::string s =
>>>> "/usr/local/src/test.dat"
>>>>
>>>> Is there a string function that will locate the last "/" in a string
>>>> and
>>>> null terminate the string on that character?
>>>>
>>>
>>> start with basic_string::rfind.
>>>
>>
>> While rfind is perfectly fine I think find_last_of states in the
>> intent ever so slightly clearer.
>
> Um, not hardly. And it's bigger and slower. rfind does exactly what's
> needed.
>
I'm not sure why you are convinced that it's "bigger and slower". If
there is more to it than the following, I would love to hear about it.
The standard requires the same behavior of both:
They both ought to behave as if they forward to their overload taking a
basic_string containing that one char.
(From 21.3.6

- Effects: Determines the highest position xpos, if possible, such that
both of the following conditions obtain:
rfind:
— xpos <= pos and xpos + str.size() <= size();
— at(xpos+I) == str.at(I) for all elements I of the string controlled by
str.
find_last_of:
— xpos <= pos and xpos < size();
— at(xpos) == str.at(I) for some element I of the string controlled by str.
With str.size() == 1 that amounts to the same.
In practice both the implementations that I checked: msvc10's and gcc
4.5's basic_string::find_last_of are a one line function, defined
inline, that does nothing but forward the call to rfind.
MSVC:
size_type find_last_of(_Elem _Ch, size_type _Off = npos) const
{ // look for _Ch before _Off
return (rfind((const _Elem *)&_Ch, _Off, 1));
}
While rfind('/') merely forwards to the same version of rfind, too:
size_type rfind(_Elem _Ch, size_type _Off = npos) const
{ // look for _Ch before _Off
return (rfind((const _Elem *)&_Ch, _Off, 1));
}
GCC:
* Note: equivalent to rfind(c, pos).
*/
size_type
find_last_of(_CharT __c, size_type __pos = npos) const
{ return this->rfind(__c, __pos); }
So find_last_of will not be faster than rfind but *usually* not slower
either. Anybody who doesn't agree that find_last_of might state the
intent clearer (and I don't feel strongly about that either; I admit
that it's hardly more than a bikeshed issue anyway :/) absolutely should
go with rfind but I'm not convinced that performance is a killer
argument here.
Have a nice day,
Stefan