what is it ?
explain briefly pleaseCode:if (phrase. find("eggplant") == string::npos) cout << "'eggplant' is not in the phrase.\n\n";
what is it ?
explain briefly pleaseCode:if (phrase. find("eggplant") == string::npos) cout << "'eggplant' is not in the phrase.\n\n";
string::npos is a position value which means "not found." Think "no position."
whats the actual value of it ?
can't we do the same thing without npos ?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
>> You're not supposed to care.
Agreed.
If you're curious, though, the actual value of it is defined as -1 converted to the type string::size_type which is an unsigned integral type.
Without even opening a book, you can find things out like this yourself with a little thinking...
printf("The value is string::npos is %s\n", string::npos) ;
oops - that compiles, but crashes... try something else
printf("The value is string::npos is %d\n", string::npos) ;
ok, that compiles, now run and see.
Todd
Mainframe assembler programmer by trade. C coder when I can.
What? You've never used -1U?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
That's where a typecast comes into play. Here is a bit from Visual Studio 2008's (and 2005's) xstring header file:
MSVC 6's xstring header file:Code:// STATIC npos OBJECT template<class _Elem, class _Traits, class _Alloc> _PGLOBAL const typename basic_string<_Elem, _Traits, _Alloc>::size_type basic_string<_Elem, _Traits, _Alloc>::npos = (typename basic_string<_Elem, _Traits, _Alloc>::size_type)(-1);
Code:template<class _E, class _Tr, class _A> const basic_string<_E,_Tr,_A>::size_type basic_string<_E,_Tr,_A>::npos = -1;
Last edited by hk_mp5kpdw; 03-24-2008 at 02:07 PM.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
>> Without even opening a book, you can find things out like this yourself with a little thinking...
True, although there is a difference between the value that one particular implementation uses and a value that is required to be used by all standards conforming implementations.
That is correct. It should be treated as an opaque value. The doc I have on it goes on for a paragraph about the typical value (-1) and the string::size_type declaration, and to not treat it like an int, blah blah blah.
Hopefully the OP's curiosity has been satisfied!
Mainframe assembler programmer by trade. C coder when I can.
>Hopefully the OP's curiosity has been satisfied!
If not we can probably go into more detail.
My best code is written with the delete key.
That would be:True, although there is a difference between the value that one particular implementation uses and a value that is required to be used by all standards conforming implementations.
in the scope of std::basic_string.Code:static const size_type npos = -1;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)