As I understand it, string::find_first_of looks though a string for any character found in cstr in the interval [pos, pos+n).
So in the code below, the substring being searched should be "con", which contains the character "c", found at position 0. So shouldn't the function return 0? Why is string::npos (not found) returned?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// C++11
#include <iostream>
#include <string>
#include <cstddef> // std::size_t
using std::string;
using std::cout;
int main ()
{
// What is the substring being searched? Isn't it "con"?
size_t idx = string("consteval").find_first_of("xyzabc", 0, 3);
cout << "Found: " << idx << "\n";
return 0;
}
It seems n specifies the number of characters of "xyzabc" (cstr) to use for searching. For n=3, we're searching for any characters in "xyz" that are found in "czonsteval". The "z" is found at position 1.
Note that it is only when you pass a const char* that you can provide a third argument. This could be useful for example if the string is not null terminated.
To answer the title question, it is the length of the array of characters whose first element is pointed to by cstr.
Peter87 wrote:
Note that it is only when you pass a const char* that you can provide a third argument. This could be useful for example if the string is not null terminated.