Thread: string search

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    81
    Rep Power
    12

    string search


    Hiya
    Here is my problem I am trying to search through a string for a character. This what I have

    std::string::size_type pos=0;
    pos = cmd.find_first_of(";",0);
    if(pos == "string::npos"){
    blah blah blah
    }

    when I go to compile it I get this error

    ISO C++ forbids comparison between pointer and integer

    telling me the line number is from the if statement. I tried type casting, and did not have any luck. Any suggestions?? Thanks
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    I'm not sure what your code is supposed to be trying to do and I don't recognize the syntax, but it appears that pos is an integer and that you are comparing it to a constant literal, "string::npos". That doesn't make any sense.

    Is npos a property of string? Is that property of type int? And are you looking for where pos equals the npos property of string? If so, then losing the quote marks should help:
    Code:
    if(pos == string::npos){
     ...
    What is that syntax, anyway? I learned C++ ten years ago; is that some new coding style that recently became fashionable or some vendor's proprietary extension?
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Originally posted by dwise1_aol
    What is that syntax, anyway? I learned C++ ten years ago; is that some new coding style that recently became fashionable or some vendor's proprietary extension?
    Actually it is ANSI standard C++. The standard had been evolving for years and was probably not there when you learned C++ (matter of fact, I didn't see it when I first learned C++ or, perhaps my book must have not been that good). string::npos is a special constant value of type string::size_type to indicate that the position was not found. A lot of the member functions such as length(), find() etc. return values of type string::size_type. I think string::size_type is typedef'd as int or unsigned int in most implementations currently.
    Last edited by Scorpions4ever; April 8th, 2003 at 10:54 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    81
    Rep Power
    12

    Not working


    I am trying to figure out if there is a ; in the string. I tried typecasting the string to an int, and it does not work correctly.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222

    Re: Not working


    Originally posted by jimo9
    I am trying to figure out if there is a ; in the string. I tried typecasting the string to an int, and it does not work correctly.
    Did my suggestion to remove the quote marks from the if-statement work?
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally posted by Scorpions4ever
    Actually it is ANSI standard C++. The standard had been evolving for years and was probably not there when you learned C++ (matter of fact, I didn't see it when I first learned C++ or, perhaps my book must have not been that good). string::npos is a special constant value of type string::size_type to indicate that the position was not found. A lot of the member functions such as length(), find() etc. return values of type string::size_type. I think string::size_type is typedef'd as int or unsigned int in most implementations currently.
    OK, static class members. Sure, we had those too back when we were coding with stone knives and bear-skins. Just wasn't familiar with applying the idea to data types. Plus, I haven't gotten around to playing with STL yet.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    81
    Rep Power
    12

    Solution


    If the string searched for is not found, it returns a negative one.
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    2
    Rep Power
    0
    I have tried the code of jimo9 without quotes and it works. The value of string::npos is just the largest integer possible on my box. This is also the value of the variable pos if the character is not found in the string.

    Therefore the solution to compare pos to 0 does not work in my case. I use a gcc compiler version 3.2

    Ciao,

    Gilles Marcou

IMN logo majestic logo threadwatch logo seochat tools logo