472,971 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

std::string, subscripting, and npos

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";
std::cout << s << '\n';

for (std::string::size_type i = 0; i != std::string::npos; ++i) {
if (s[i] == '/') {
s[i] = '\\';
}
}

std::cout << s << '\n';
}
If I change "std::string::npos" to "s.length()" then of course the
program works correctly. Why doesn't std::string::npos work in this
context?
--
Marcus Kwok
Jan 27 '06 #1
7 4822
On Fri, 27 Jan 2006 21:06:31 +0000 (UTC), ri******@gehennom.net
(Marcus Kwok) wrote:
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";
std::cout << s << '\n';

for (std::string::size_type i = 0; i != std::string::npos; ++i) {
if (s[i] == '/') {
s[i] = '\\';
}
}

std::cout << s << '\n';
}
If I change "std::string::npos" to "s.length()" then of course the
program works correctly. Why doesn't std::string::npos work in this
context?


std::string::npos is usually just an alias for ((unsigned)(-1)). You
are therefore incrementing i way past the end of the string's length.

std::string::npos is returned by most string functions which involve
some kind of find, and which would normally return the index or
position of the element being sought. Since there can never be such a
position, this value was chosen to indicate "not found" or not
present.

--
Bob Hairgrove
No**********@Home.com
Jan 27 '06 #2
Marcus Kwok wrote:
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";
std::cout << s << '\n';

for (std::string::size_type i = 0; i != std::string::npos; ++i) {
if (s[i] == '/') {
s[i] = '\\';
}
}

std::cout << s << '\n';
}
If I change "std::string::npos" to "s.length()" then of course the
program works correctly. Why doesn't std::string::npos work in this
context?


'npos' is a constant usually defined as (size_type)(-1). Its specific
purpose is to indicate "not found" condition when returning from 'find'
members and the "end" position when using 'substr'.

You mistakenly used it as if it is different for every 'string' object.
It's not. IIRC it's a static member.

V
Jan 27 '06 #3

"Marcus Kwok" <ri******@gehennom.net> wrote in message
news:dr**********@news-int2.gatech.edu...
std::string::npos is described in _TC++PL:SE_ (Section 20.3.4) as the
"all characters" marker.
Its meaning depends upon context. It's most often used to
indicate 'not found' with e.g. 'find()'.
I tried to use it this way, but my program
crashes:

#include <iostream>
#include <string>

int main()
{
std::string s = "hi/hello/now";
std::cout << s << '\n';

for (std::string::size_type i = 0; i != std::string::npos; ++i) {
'npos' is not a valid index for a string. Even if you use the
meaning 'all characters', what would that value as an index
mean? The book doesn't say 'how many characters' does it? :-)
(and even if it did, then the highest valid index would be
'how many, less one' (because first index is 0).)

The valid index values are those integers in the set beginning with
zero and ending with 'string::size() - 1', inclusive.

'size() - 1' can never be equal to 'npos'. That's part of
'npos's definition. The value of 'npos' must be chosen by
an implementation to be some value that is *not* a valid
index. That's how e.g. 'find()' (which returns an index
value), can distinguish between a found element and 'not found'.

Once 'i's value passes 'size() - 1', using it as an index
will give undefined behavior. (The 'at()' member functinon
would throw an exception).

Write this instead:

for (std::string::size_type i = 0; i != s.size(); ++i) {
if (s[i] == '/') {
s[i] = '\\';
}
}

std::cout << s << '\n';
}
If I change "std::string::npos" to "s.length()"
Note that 'size()' and 'length()' are two functions which
have exactly the same effect.
then of course the
program works correctly. Why doesn't std::string::npos work in this
context?


Because you're trying to use it for something other than
it's defined purpose. Like trying to cut a watermelon with
a feather. :-)

-Mike
Jan 27 '06 #4

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:eb***************@newsread1.mlpsca01.us.to.ve rio.net...
If I change "std::string::npos" to "s.length()" then of course the
program works correctly. Why doesn't std::string::npos work in this
context?


'npos' is a constant usually defined as (size_type)(-1). Its specific
purpose is to indicate "not found" condition when returning from 'find'
members and the "end" position when using 'substr'.

You mistakenly used it as if it is different for every 'string' object.
It's not. IIRC it's a static member.


I suspect that Marcus misunderstood after seeing npos used in
a different context, e.g. 'string::substr()'s second parameter
specifies a default value of 'npos', meaning 'all'.

std::string s("hello");
std::string sub(s.substr(0)); // now s == sub

-Mike
Jan 27 '06 #5
Bob Hairgrove <in*****@bigfoot.com> wrote:
On Fri, 27 Jan 2006 21:06:31 +0000 (UTC), ri******@gehennom.net
(Marcus Kwok) wrote:
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";
std::cout << s << '\n';

for (std::string::size_type i = 0; i != std::string::npos; ++i) {
if (s[i] == '/') {
s[i] = '\\';
}
}

std::cout << s << '\n';
}
If I change "std::string::npos" to "s.length()" then of course the
program works correctly. Why doesn't std::string::npos work in this
context?


std::string::npos is usually just an alias for ((unsigned)(-1)). You
are therefore incrementing i way past the end of the string's length.

std::string::npos is returned by most string functions which involve
some kind of find, and which would normally return the index or
position of the element being sought. Since there can never be such a
position, this value was chosen to indicate "not found" or not
present.


Thanks. I was thinking maybe it would be similar to the .end()
iterator (as in, 1 greater than the last element).

--
Marcus Kwok
Jan 27 '06 #6
On Fri, 27 Jan 2006 21:06:31 +0000 (UTC) in comp.lang.c++,
ri******@gehennom.net (Marcus Kwok) wrote,
std::string::npos is described in _TC++PL:SE_ (Section 20.3.4) as the
"all characters" marker.


That statement refers specifically to the use of npos as an argument
to the string constructor that creates a substring of an existing
string. Your code is not doing anything even slightly resembling
that. Give me a break.

Jan 27 '06 #7
> Thanks. I was thinking maybe it would be similar to the .end()
iterator (as in, 1 greater than the last element).


You could always just use an iterator.
Jan 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
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(...
1
by: Chris Mantoulidis | last post by:
PROBLEM: I'm having some weird problems with string::find() (in ParamGenerate()), but since I'm not sure if that is the source of all bad output in my program, I'm posting least code that's...
4
by: Christopher | last post by:
I am using std::string to parse a command given by the user, I don't understand why the following snippet is not working as expected. string buffer, // store commands from...
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...
2
by: FBergemann | last post by:
if i compile following sample: #include <iostream> #include <string> int main(int argc, char **argv) { std::string test = "hallo9811111z"; std::string::size_type ret;
8
by: Marcus Kwok | last post by:
Is std::string::npos portably able to be incremented? For example, I want to insert some text into a string. If a certain character is found, I want to insert this text immediately after this...
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.