C++: is string.empty() always equivalent to string == ""? -


Can I create an assumption that

  std :: string str; ... /  

Do something for the following statement is always true?

  (str.empty () == (str == ""))  

Answer

Yes, the implementation is relevant to bits / basic_string.h , basic_string & lt; _CharT, _Traits, _Alloc & gt; Code for :

True if the string is empty I = I == "" * / Bull blank () Const (return it - > Size () == 0;}

discussion

Although two forms are equivalent to std :: string , you May be to use <.> .empty () because it is more common.

In fact, the comment is that if you type std: : Std :: string instead of wstring , then == "" will also not be compiled, because you have the wchar_t You can not compare a string with > four . However, this is not directly relevant to your basic question, and I'm 99% sure that you do not have std :: wstring


Comments