Thread: string::npos

  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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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,005
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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,366
    >> 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
    Chappell Hill, Texas
    Posts
    2,332
    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
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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,817
    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,366
    >> 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
    Chappell Hill, Texas
    Posts
    2,332
    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.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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