what is it ?
explain briefly pleaseCode:if (phrase. find("eggplant") == string::npos)
cout << "'eggplant' is not in the phrase.\n\n";
Printable View
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 ?
>> 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
What? You've never used -1U? :p
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;
>> 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!
>Hopefully the OP's curiosity has been satisfied!
If not we can probably go into more detail. ;)
That would be:Quote:
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;