I'm trying to find the last occurrence of a char in a range from a large text, so I have to specify off
and count
to limit the range.
string s = "a abc test";
cout << s.find_last_of(" ", 5, 2);//works, result 5
This works pretty well, but at first, I tried rfind()
.
I tried either rfind(" ", 5, 2)
or rfind(" ", 3, 4)
, but both return npos
.
Why?
The 2 methods are different:
std::string::find_last_of
is used to:
Finds the last character equal to one of characters in the given character sequence.
(emphasis is mine)
Whereas std::string::rfind
is used to find a given substring (not just one of its characters):
Also note that with std::string::rfind
the count
parameter determines the length (i.e. number of characters) of the substring to search for:
count - length of substring to search for
Therefore in order to find a string of 1 space character, you need to pass 1
for count
.
std::string::rfind
also has an overload that does not require count
and simply serached for the substring as given.
In your case you can also use a 3rd overload that does not require a position and simply searches the substring from the right.
#include <string>
#include <iostream>
int main() {
std::string s = "a abc test";
//---------------------------v---------
std::cout << s.rfind(" ", 5, 1) << "\n"; // prints 5
std::cout << s.rfind(" ", 5) << "\n"; // prints 5
std::cout << s.rfind(" ") << "\n"; // prints 5
}
Output:
5
5
5
(Note: the live demo is with gcc because Godbolt does not support an executor for MSVC at the moment, but I get the same result with VS2022).
A side note:
Your usage of count
in find_last_of
is also based on incorrect interpretation of the parameter (despite the fact that you got the expected result). It actually refers to the "length of character string identifying characters to search for", and not to the source string where the search is done.
And BTW - if you passed 3
or more as count
it was also triggering UB (undefined behavior), because your s
is " "
which is char[2]
:
If [s, s + count) is not a valid range, the behavior is undefined.