string member functions

This is a discussion on string member functions within the C++ Programming forums, part of the General Programming Boards category; Hey guys, Curiously...I just read that a string member function (for example, find() in my case) returns a -1 if ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    string member functions

    Hey guys,
    Curiously...I just read that a string member function (for example, find() in my case) returns a -1 if it doesnt find the specified target. If this is true (which I can only assume it is b/c I read it in a book ...OH...and tested it myself hehe) why does the following work?
    Code:
    if (someString.find(target))  //assuming target is NOT in the string
          cout << "found it!"
    else
          cout << "didnt find it...";
    It didnt find the target...yet it printed "found it" to the screen...it just seems a little backwards to me. Any ideas why it was implemented this way? The only logic I can see is so when you set up test conditions you can logically think it out as
    Code:
    if (!(str.find(target)) //if it doesnt find target
    ...Any replies would be cool...thanks a lot. -Chap

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I just read that a string member function (for example, find() in
    >my case) returns a -1 if it doesnt find the specified target
    Your source is incorrect. If string::find doesn't find the target, it returns string::npos. So the correct test would be:
    Code:
    if ( somestring.find ( target ) != string::npos )
      cout<<"Found it!"<<endl;
    else
      cout<<"Not found"<<endl;
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    82
    If you're looking for an explanation, many (most/all?) implementations define npos as such (in logic if not in syntax):
    Code:
    size_type npos = -1;
    size_type is going to be some type of unsigned integer, so it wraps around to the largest value size_type could represent. And Prelude's code demonstrates how to check for it.
    Last edited by AH_Tze; 04-06-2005 at 08:36 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The find function does not return true or false depending on whether the string is found. Instead it returns the index where the string was found.

    The index can be 0 or any positive integer. When using boolean logic like you were trying to do, 0 means false and non-zero means true. So, that won't work with find since the string might be found at index 0. That is why you must compare it to npos (or -1, since npos is defined as -1).

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    82
    The -1 is an implementation detail. If you used it, you'd be obfuscating what you're doing and assuming the compiler will give it the correct type.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    it just seems a little backwards to me. Any ideas why it was implemented this way? The only logic I can see is so when you set up test conditions you can logically think it out as

    if (!(str.find(target)) //if it doesnt find target
    I would have to agree--it does seem backwards.

    Quote Originally Posted by Prelude
    >I just read that a string member function (for example, find() in
    >my case) returns a -1 if it doesnt find the specified target
    Your source is incorrect. If string::find doesn't find the target, it returns string::npos. So the correct test would be:
    Code:
    if ( somestring.find ( target ) != string::npos )
      cout<<"Found it!"<<endl;
    else
      cout<<"Not found"<<endl;
    Why not simply:

    if(somestring.find(target))

    as the op used?
    Last edited by 7stud; 04-06-2005 at 11:00 PM.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    82
    Quote Originally Posted by 7stud
    Why not simply:

    if(somestring.find(target))

    as the op used?
    You're asking for a bool but getting string::size_type.
    It will always evaluate to true except for when the target string is the first character of someString.
    Last edited by AH_Tze; 04-06-2005 at 11:47 PM.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by AH_Tze
    It will always evaluate to true except for when the target string is the first character of someString.
    I see: the if condition always evaluates to true whether find() finds the target or not. There is only one exception: if the target happens to be on the front of the string, in which case find() will return the position 0, and the if condition will evaluate to false. Therefore, to work properly the if statement has to compare the return value of find() to string::npos.

    Last edited by 7stud; 04-07-2005 at 12:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. a menu system with string functions
    By pxleyes in forum C Programming
    Replies: 11
    Last Post: 04-09-2004, 12:08 AM
  4. Data member initialization
    By Fyodorox in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2002, 12:09 AM

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