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

Can I assume that this

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

Is the following statement always true?

 (str.empty() == (str == "")) 
+42
c ++ string
Jan 27 '09 at 13:10
source share
7 answers

Answer

Yes. Here is the corresponding 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 if these two forms are equivalent for std::string , you can use .empty() because it is more general.

Indeed, JF Sebastian notices that if you switch to using std::wstring instead of std::string , then =="" will not even compile, because you cannot compare the wchar_t string with one of char . This, however, is not directly related to your original question, and I'm 99% sure that you will not switch to std::wstring .

+48
Jan 27 '09 at 13:17
source share

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

size_type size() const;

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

bool empty() const;

Returns: size() == 0

However, section 18 of constructor 21.3.1 basic_string states that the character type assignment operator uses traits::length() to set the length of the monitored sequence so that you can get something strange if you use another specialization std::basic_string<> .

I think the 100% correct operator 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 test different things. str.empty() checks if the string is empty, where the other checks for equality on an empty string in C style. I would use what is more suitable for what you are trying to do. If you want to know that the string is empty, use str.empty() .

+11
Jan 27 '09 at 14:10
source share

str.empty () is never slower, but can be faster than str == "". It depends on the implementation. Therefore, you should use str.empty () just in case.

This is a bit like using ++ i instead of i ++ to increment the counter (assuming you don't need the result of the increment operator itself). Your compiler can optimize, but you donโ€™t lose anything with ++ I and you can win something, so you better use ++ i.

Besides performance issues, the answer to your question is: yes; both expressions are logically equivalent.

+7
Jan 27 '09 at 13:54
source share

Yes (str.empty() == (str == "")) always * true for std::string . But remember that a string can contain the characters '\0' . Thus, although the expression s == "" may be false, s.c_str() may still return an empty C string. For example:

 #include <string> #include <iostream> using namespace std; void test( const string & s ) { bool bempty = s.empty(); bool beq = std::operator==(s, ""); // avoid global namespace operator== const char * res = (bempty == beq ) ? "PASS" : "FAIL"; const char * isempty = bempty ? " empty " : "NOT empty "; const char * iseq = beq ? " == \"\"" : "NOT == \"\""; cout << res << " size=" << s.size(); cout << " c_str=\"" << s.c_str() << "\" "; cout << isempty << iseq << endl; } int main() { string s; test(s); // PASS size=0 c_str="" empty == "" s.push_back('\0'); test(s); // PASS size=1 c_str="" NOT empty NOT == "" s.push_back('x'); test(s); // PASS size=2 c_str="" NOT empty NOT == "" s.push_back('\0'); test(s); // PASS size=3 c_str="" NOT empty NOT == "" s.push_back('y'); test(s); // PASS size=4 c_str="" NOT empty NOT == "" return 0; } 

** prohibiting operator== overloading in the global namespace, as others noted *

+3
Jan 27 '09 at 21:29
source share

Some implementations may test the null character as the first character in a string, resulting in a slight increase in speed when calculating the size of a string.

I believe this is not common.

+1
Jan 27 '09 at 13:25
source share

Usually yes.

But if someone decides to redefine the operator, then all bets will not be:

 bool operator == (const std::string& a, const char b[]) { return a != b; // paging www.thedailywtf.com } 
+1
Jan 27 '09 at 13:32
source share

Yes, this is equivalent, but it allows the base code to change the implementation of what empty () actually means, depending on OS / Hardware /, and doesnโ€™t affect your code at all. There is a similar practice in Java and .NET.

0
Jan 27 '09 at 13:20
source share



All Articles