473,836 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::string.fin d_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::si ze_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_no t_of("987654321 0");
if (ret == std::string::np os){
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("987654 3210", 5, 3);
if (ret == std::string::np os){
std::cout << "is npos" << std::endl;
}
else {
std::cout << "is NOT npos, but <" << ret << ">" <<
std::endl;
}

}

the result is:

string is: <hallo9811111 z>
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_o f() 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 5523

<FB********@web .deskrev i meddelandet
news:11******** *************@h 48g2000cwc.goog legroups.com...
if i compile following sample:
std::cout << "1st check ->";
ret = test.substr(5,3 ).find_first_no t_of("987654321 0");
std::cout << "2nd check ->";
ret = test.find_first _not_of("987654 3210", 5, 3);
>
string is: <hallo9811111 z>
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_o f() 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
24212
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. Regards Giampiero
13
1893
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 one_line; .. .. .. idx = one_line.find('\t', 0);
11
3679
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( (idx=str.find_first_of(',')) >= 0 ) { str.replace( idx, 1, "" ); str.insert( idx, " or " ); }
18
2436
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 description of how each works. I've been trying Google for the last 20 minutes but can't get anything decent. Thanks.
1
4418
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 type std:string): ------
7
4872
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 main() { std::string s = "hi/hello/now";
13
3675
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 <string>
11
4296
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. Yick. All I could come up with is: #include <string> int main() { std::string text;
7
4274
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 expression") or Comeau Online ("constant value is not known"). If I replace
0
10862
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10560
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10607
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9389
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6986
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4468
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.