468,513 Members | 1,760 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

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

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

Aug 7 '06 #1
2 5153

<FB********@web.deskrev i meddelandet
news:11*********************@h48g2000cwc.googlegro ups.com...
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
Aug 7 '06 #2
Hi Bo,

Bo Persson schrieb:
>
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

Aug 8 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Giampiero Gabbiani | last post: by
13 posts views Thread by Dave | last post: by
11 posts views Thread by Christopher Benson-Manica | last post: by
18 posts views Thread by JKop | last post: by
1 post views Thread by Tero Toivanen | last post: by
7 posts views Thread by Marcus Kwok | last post: by
13 posts views Thread by arnuld | last post: by
11 posts views Thread by Christopher Pisz | last post: by
7 posts views Thread by Hendrik Schober | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.