std::string.find_first_not_of(size_type index, size_type num)

F

FBergemann

if i compile following sample:

#include <iostream>
#include <string>

int main(int argc, char **argv)
{

std::string test = "hallo9811111z";
std::string::size_type ret;

std::cout << "string is: <" << test << ">" << std::endl;
std::cout << "string.substr(5,3) is: <" << test.substr(5,3) <<
">" << std::endl;

std::cout << "1st check ->";
ret = test.substr(5,3).find_first_not_of("9876543210");
if (ret == std::string::npos){
std::cout << "is npos" << std::endl;
}
else {
std::cout << "is NOT npos, but <" << ret << ">" <<
std::endl;
}

std::cout << "2nd check ->";
ret = test.find_first_not_of("9876543210", 5, 3);
if (ret == std::string::npos){
std::cout << "is npos" << std::endl;
}
else {
std::cout << "is NOT npos, but <" << ret << ">" <<
std::endl;
}

}

the result is:

string is: <hallo9811111z>
string.substr(5,3) is: <981>
1st check ->is npos
2nd check ->is NOT npos, but <7>


I wonder, why 2nd check doesn't result in string::npos as well.
The manpage of fin_first_not_of() says:

"... returns the index of the first character within the current string
that does not match any character in str, beginning the search at index
and searching at most num characters, string::npos if nothing is found.
...."

???

Regards!

Frank
 
B

Bo Persson

if i compile following sample:
std::cout << "1st check ->";
ret = test.substr(5,3).find_first_not_of("9876543210");
std::cout << "2nd check ->";
ret = test.find_first_not_of("9876543210", 5, 3);
string is: <hallo9811111z>
string.substr(5,3) is: <981>
1st check ->is npos
2nd check ->is NOT npos, but <7>


I wonder, why 2nd check doesn't result in string::npos as well.


Beacuse of a crazy interface!
The manpage of fin_first_not_of() says:
"... returns the index of the first character within the current
string
that does not match any character in str, beginning the search at
index
and searching at most num characters, string::npos if nothing is
found.

The problem here is that 'index' is applied to the string 'test',
while 'num characters' tells the size of the matching parameter.

So, in the first case you pick 3 chars starting at position 5 in test,
and match against "9876543210".

In the second case, you start at position 5 in test, and match against
the 3 characters "987".


Totally obvious! :))


Bo Persson
 
F

FBergemann

Hi Bo,

Bo said:
Beacuse of a crazy interface!

The problem here is that 'index' is applied to the string 'test',
while 'num characters' tells the size of the matching parameter.

So, in the first case you pick 3 chars starting at position 5 in test,
and match against "9876543210".

In the second case, you start at position 5 in test, and match against
the 3 characters "987".


Totally obvious! :))

Ouh! - thanks for the hint.
That should be explained better in the manual-pages - at least in the
ones i found in the net.

Thanks again!

Regards!

Frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top