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 2 5511
<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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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);
|
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 " );
}
|
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.
|
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):
------
| |
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";
|
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>
|
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;
|
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
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
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
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |