By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a

<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

P: n/a
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.