Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts

    C++ Find Function

    When i use the find function does it output an interger value?

    EX:

    Code:
    string str1;
    
    str1 = "hello";
    So if i had the function in there
    Code:
     cout << str1.find("e");
    it would output 1 for the position of the "e" in the string correct? Is this 1 considered an integer and can I use it to compare values?

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    When you post a question specific to a language, it helps to tell us the version / compiler, if applicable. c++.net has .net classes. This looks like standard from <string.h>...

    So, here you go.
    http://www.cppreference.com/cppstring/find.html

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    The return type is std::string::size_t. I don't know what portability guarantees you get with that but as far as I've ever determined it's always a typedef of unsigned long. So, yeah, you can use it for regular integer stuff but mind that you don't make comparisons when the result is std::string::npos, which is == ULONG_MAX (also probably implementation-defined, but that's the only sensible value for it if the container is unsigned)

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    std::string::size_t.

    what the heck does this mean? thee book i'm reading does not mention anything that looks remotly like this

    maybe i should get a new book

  • #5
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by srule_ View Post
    std::string::size_t.

    what the heck does this mean? thee book i'm reading does not mention anything that looks remotly like this

    maybe i should get a new book
    size_t is a typedef that the string class uses when it returns the length, or positions from find, etc. Your book might just assume it is an int, which can work but most versions of the STL have it mapped to an unsigned long like ralph said.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •