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?? :confused:
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; }
Quote:
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
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.
Re: std::string - how is this working?
Thanks for the info guys! :thumb:
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