Home > Net >  Am I misunderstanding find_first_not_of?
Am I misunderstanding find_first_not_of?

Time:07-11

Consider the following program:

#include <string>
#include <iostream>

int main() {
    std::string token {"abc"};
    const char* option_name_valid_chars =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz"
        "0123456789"
        "_-";
    auto pos = token.find_first_not_of(option_name_valid_chars, 0, 1);
    std::cout << pos << std::endl;
}

This prints 0 (GodBolt), while I expect it to print the value of npos: All 1 characters at positions starting from 0 are valid, so the "first" position of an invalid char is npos.. or am I misunderstanding the semantics of find_first_not_of()?

CodePudding user response:

The reason of your confusion is that you are incorrectly considering the meaning of the third argument (parameter) in this call

auto pos = token.find_first_not_of(option_name_valid_chars, 0, 1);
                                                              ^^^

Actually it determines the length of the substring of the string option_name_valid_chars.

In fact the above call is equivalent to

auto pos = token.find_first_not_of( "A", 0 );
      

According to the C 17 Standard the method is defined the following way

size_type find_first_not_of(const charT* s, size_type pos, size_type n) const;

4 Returns: find_first_not_of(basic_string_view<charT, traits>(s, n), pos).

As you can see the third parameter is used as the length of the string s,

CodePudding user response:

As written in CPP reference here:

Finds the first character equal to none of the characters in the given character sequence. The search considers only the interval [pos, size()). If the character is not present in the interval, npos will be returned.

In version 2 of the function, you may specify the count of you characters for comparison. So, in your case 1. With that, you will not compare to

"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"_-"

but only to 'A'.

And so, 0 will be returned, since 0 is the index of the first none 'a' character.

Remove the count. Then it will work.

CodePudding user response:

You're misunderstanding count, the third parameter.

What you thought it means: Limit the haystack - limit the number of characters of token to search.

What it actually means: Limit the needle - limit the of characters of option_name_valid_chars to consider.

What you should do is:

  1. create a string_view of your substring of interest (this requires C 17; otherwise you could make a copy with the token.substr(...))
  2. Search your substring without specifying the count.
  3. Check against npos.
  • Related