473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 5456

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Giampiero Gabbiani | last post by:
Hi to all, Is there a simple way to implement a strip algorithm on std::string using STL? I'm sure that it's possible to implement it using some transform, but my knowledge of STL is POOR. ...
13
by: Dave | last post by:
Can anybody tell me why the code below should produce the error below? I'm going nuts here! Am I missing something incredibly obvious and simple? std::string::size_type idx; std::string...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
18
by: JKop | last post by:
Can some-one please point me to a nice site that gives an exhaustive list of all the memberfunctions, membervariables, operators, etc. of the std::string class, along with an informative...
1
by: Tero Toivanen | last post by:
Dear experts, I am doing code to Solaris 9 system with C++. I get every now and then segmentation fault in the following code that removes heading and trailing white spaces (mLineStr is of...
7
by: Marcus Kwok | last post by:
std::string::npos is described in _TC++PL:SE_ (Section 20.3.4) as the "all characters" marker. I tried to use it this way, but my program crashes: #include <iostream> #include <string> int...
13
by: arnuld | last post by:
/* C++ Primer 4/e * section 3.2 - String Standard Library * exercise 3.10 * STATEMENT * write a programme to strip the punctation from the string. */ #include <iostream> #include...
11
by: Christopher Pisz | last post by:
Is std::string::npos always going to be less than any std::string 's size()? I am trying to handle a replacement of all occurances of a substr, in which the replacement also contains the substr....
7
by: Hendrik Schober | last post by:
Hi, this #include <string> class test { typedef std::string::size_type size_type; static const size_type x = std::string::npos; }; doesn't compile using either VC9 ("expected constant...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.