CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2002
    Posts
    1,417

    string find_first_of fails

    why does this fail (VC6 on W2K)

    Code:
    #include <string>
    
    int main()
    {
       string spath = "\\windows\\something";
    
       if(spath.find_first_of("*?") >= 0)
       {
           // this is always true 
           // remove the wild cards
       }
    }
    But this works ok:

    Code:
    #include <string>
    int main()
    {
       string spath = "\\windows\\something";
       int pos = spath.find_first_of("*?");
       if(pos >= 0)
       {
           // remove the wild cards
       }
    }

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    find_first_of returns type of size_type, which looks like it's unsigned int. Of course it's always >= 0. In the second example, you are casting it to an int.

    Jeff

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    When using std::string search/find functions, if not found
    a special value is returned ... string::npos

    So your check should look like this :

    Code:
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::string spath = "\\windows\\something";
    
        if(spath.find_first_of("*?") != std::string::npos)
        {
           std::cout << "found wildcard" << std::endl;
        }
    
        // edit adding further clarification ...
    
    
       int pos1 = spath.find_first_of("?*");
       cout << "as an int : " << pos1 << endl; 
    
       string::size_type pos2 = spath.find_first_of("?*");
       cout << "as a string::size_type : " << pos2 << endl; 
    
       cout << "string::npos = " << string::npos << endl;
    
    
       return 0;
    }
    Last edited by Philip Nicoletti; December 11th, 2002 at 12:55 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured