CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    std::string - how is this working?

    Firstly, is std::string the same thing as basic_string? The reason I'm asking is that I can never find anything on MSDN about std::string - but basic_string seems very similar.

    Anyway, here's my question - Yesterday I was looking at some code that I wrote a long time ago. It does some string comparisons and I realised that I should probably have written something like this:-

    Code:
    #include <string>
    
    using namespace std;
    
    void MyFunc ( string someString, char* compareString )
    {
        if ( 0 == someString.compare( string( compareString )))
            ; do something here....
    }
    whereas in fact, I did it this way:-

    Code:
    #include <string>
    
    using namespace std;
    
    void MyFunc ( string someString, char* compareString )
    {
        if ( someString == compareString )
            ; do something here....
    }
    That code appears to work just fine - even though this MSDN article seems to suggest that basic_string has no equality operators.

    Am I looking at the wrong MSDN article? Or is it out of date? Or have I somehow been kidding myself all these years and the code doesn't really work??
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: std::string - how is this working?

    Quote Originally Posted by John E
    Firstly, is std::string the same thing as basic_string?
    It is is the following typedef:
    Code:
    namespace std { typedef basic_string<char> string; }
    The reason I'm asking is that I can never find anything on MSDN about std::string - but basic_string seems very similar.
    You need to get a more thorough reference to the C++ standard classes than MSDN. Something like "The C++ Standard Library" by Jossutis.

    That article doesn't show you not only operator ==, but operator <, operator >, etc. which are all valid.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: std::string - how is this working?

    std::basic_string is a template.
    std::string is an instantiation of this template, in simplified form (ignoring traits arguments) it boils down to a definition like
    Code:
    typedef std::basic_string<char> std::string;
    std::basic_string::operator== (and thus std::string::operator==) is defined and works as you expect. If MSDN is telling something different, it probably does that to lie about superiority of CString or something along those lines.

    Edit: Looks like Paul beat me to an answer.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    Re: std::string - how is this working?

    Thanks for the info guys!
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  5. #5
    Join Date
    Jan 2001
    Posts
    253

    Re: std::string - how is this working?

    The problem with MSDN is that it isn't organized in a way that you can easily find things or learn about the C++ language. The particular article that was referenced in the first post: MSDN article gives information about the members of basic_string. The operator== (and other comparison operators) are not members of basic_string, but are free templated functions that take basic_string arguments.

    The link: <string> Members gives information about the comparison operators.

    If you know where to look, the MSDN library generally has the information. It just doesn't provide any easy way to find it...

    Best regards,
    John

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