Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

string

Can I make an assumption that given

std::string str; ... // do something to str 

Is the following statement is always true?

(str.empty() == (str == "")) 
like image 446
Michael Avatar asked Jan 27 '09 13:01

Michael


People also ask

Is string empty equal to?

What is an “empty” String in Java? “An empty String in Java means a String with length equal to zero.” If a String is empty that means the reference variable is referring to a memory location holding a String of length equal to zero.

Is an empty string considered a string?

An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .

Is empty string and null same in C?

The value null represents the absence of any object, while the empty string is an object of type String with zero characters. If you try to compare the two, they are not the same.


2 Answers

Answer

Yes. Here is the relevant implementation from bits/basic_string.h, the code for basic_string<_CharT, _Traits, _Alloc>:

  /**    *  Returns true if the %string is empty.  Equivalent to *this == "".    */   bool   empty() const   { return this->size() == 0; } 

Discussion

Even though the two forms are equivalent for std::string, you may wish to use .empty() because it is more general.

Indeed, J.F. Sebastian comments that if you switch to using std::wstring instead of std::string, then =="" won't even compile, because you can't compare a string of wchar_t with one of char. This, however, is not directly relevant to your original question, and I am 99% sure you will not switch to std::wstring.

like image 190
A. Rex Avatar answered Sep 20 '22 19:09

A. Rex


It should be. The ANSI/ISO standard states in 21.3.3 basic_string capacity:

size_type size() const;

Returns: a count of char-like objects currently in the string.

bool empty() const;

Returns: size() == 0

However, in clause 18 of 21.3.1 basic_string constructors it states that the character-type assignment operator uses traits::length() to establish the length of the controlled sequence so you could end up with something strange if you are using a different specialization of std::basic_string<>.

I think that the 100% correct statement is that

(str.empty() == (str == std::string())) 

or something like that. If you haven't done anything strange, then std::string("") and std::string() should be equivalent

They are logically similar but they are testing for different things. str.empty() is checking if the string is empty where the other is checking for equality against a C-style empty string. I would use whichever is more appropriate for what you are trying to do. If you want to know if a string is empty, then use str.empty().

like image 30
D.Shawley Avatar answered Sep 20 '22 19:09

D.Shawley