string::npos

This is a discussion on string::npos within the C++ Programming forums, part of the General Programming Boards category; what is it ? Code: if (phrase. find("eggplant") == string::npos) cout << "'eggplant' is not in the phrase.\n\n"; explain briefly ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    string::npos

    what is it ?

    Code:
    if (phrase. find("eggplant") == string::npos)
      cout << "'eggplant' is not in the phrase.\n\n";
    explain briefly please

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    string::npos is a position value which means "not found." Think "no position."

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    whats the actual value of it ?

    can't we do the same thing without npos ?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by manzoor View Post
    whats the actual value of it ?
    You're not supposed to care.
    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.*

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by manzoor View Post
    whats the actual value of it ?
    Who knows. The whole point of having it is so you don't care.

    can't we do the same thing without npos ?
    No.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Without even opening a book, you can find things out like this yourself with a little thinking...

    printf("The value is string::npos is &#37;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
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by Daved View Post
    >> 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.
    Err, I believe it has to be signed to be -1.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.*

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Todd Burch View Post
    Err, I believe it has to be signed to be -1.
    I think that's what was meant by:
    converted to the type string::size_type which is an unsigned integral type
    Basically something like this:
    Code:
    typedef  static_cast<size_type>( -1 )   npos;

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by Todd Burch View Post
    Err, I believe it has to be signed to be -1.
    That's where a typecast comes into play. Here is a bit from Visual Studio 2008's (and 2005'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);
    MSVC 6's xstring header file:
    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

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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.

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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!
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    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 would be:
    Code:
    static const size_type npos = -1;
    in the scope of std::basic_string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Argh! *pulls out hairs* Please help me.
    By Athildur in forum C++ Programming
    Replies: 14
    Last Post: 04-04-2007, 12:30 PM
  2. String
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 10-30-2005, 12:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21