What does the "n" argument of std::string::find_first_of(cstr, pos, n) mean?

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;
}

Output
Found: 4294967295


Example is taken from here [1].

[1] https://en.cppreference.com/w/cpp/string/basic_string/find_first_of

Edited

Looking at this example:
 
string("czonsteval").find_first_of("xyzabc", 0, 3); // Returns 1 

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.

If we do:
 
string("czonsteval").find_first_of("xyzabc", 0, 2); // Returns npos 

We searching for anyone of characters "xy" in "czonsteval" which does not exist. So npos is returned.

I think the documentation and example code on cppreference and cplusplus is pretty ambiguous when discussing this parameter :(
Last edited on
To answer the title question, it is the length of the array of characters whose first element is pointed to by cstr.
mbozzi is right.

This expression
 
string("consteval").find_first_of("xyzabc", 0, 3)
means the same as
 
string("consteval").find_first_of(string("xyzabc", 3), 0)

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.
mbozzi wrote:
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.

Well put, thanks.
Last edited on
ElusiveTau wrote:
I think the documentation and example code on cppreference and cplusplus is pretty ambiguous when discussing this parameter :(


cplusplus wrote:
size_t find_first_of (const char* s, size_t pos, size_t n) const;

s
Pointer to an array of characters.
If argument n is specified (3), the first n characters in the array are searched for.


cppreference wrote:
1
2
constexpr size_type find_first_of( const CharT* s,
                                   size_type pos, size_type count ) const;

Finds the first character equal to one of the characters in the range [s, s+count). This range can include null characters.

count - length of character string identifying characters to search for
s - pointer to a character string identifying characters to search for

I might look those in biased way, but they don't seem terribly ambiguous.

When one would use a range that contains nulls?
Topic archived. No new replies allowed.