dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: String class question

  1. #1
    Join Date
    May 2006
    Posts
    102

    String class question

    Say I have the following code:



    string dog;
    char cat;
    cin >> cat;

    if (dog.find(cat)!=string::npos)
    cout << "Yup, " << cat << " is in the dog string";



    I know what the code does, however, I'm a bit fuzzy on why the if statement would return either true or false. Could anyone explain the string::npos thing to a newb like me (my book is a bit vague on the subject).

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,767

    Re: String class question

    The various find() member functions of std::string return the index of (the first character of) the matching portion of the string, and std::string::npos if it is not found. So, if the return value is not equal to std::string::npos then there must have been a match.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2006
    Posts
    102

    Re: String class question

    Ah, thanks

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)