Difference between string.empty and string[0] == '\0'

C++String

C++ Problem Overview


Suppose we have a string

std::string str; // some value is assigned

What is the difference between str.empty() and str[0] == '\0'?

C++ Solutions


Solution 1 - C++

C++11 and beyond

string_variable[0] is required to return the null character if the string is empty. That way there is no undefined behavior and the comparison still works if the string is truly empty. However you could have a string that starts with a null character ("\0Hi there") which returns true even though it is not empty. If you really want to know if it's empty, use empty().


Pre-C++11

The difference is that if the string is empty then string_variable[0] has undefined behavior; There is no index 0 unless the string is const-qualified. If the string is const qualified then it will return a null character.

string_variable.empty() on the other hand returns true if the string is empty, and false if it is not; the behavior won't be undefined.


Summary

empty() is meant to check whether the string/container is empty or not. It works on all containers that provide it and using empty clearly states your intent - which means a lot to people reading your code (including you).

Solution 2 - C++

Since C++11 it is guaranteed that str[str.size()] == '\0'. This means that if a string is empty, then str[0] == '\0'. But a C++ string has an explicit length field, meaning it can contain embedded null characters.

E.g. for std::string str("\0ab", 3), str[0] == '\0' but str.empty() is false.

Besides, str.empty() is more readable than str[0] == '\0'.

Solution 3 - C++

Other answers here are 100% correct. I just want to add three more notes:

empty is generic (every STL container implements this function) while operator [] with size_t only works with string objects and array-like containers. when dealing with generic STL code, empty is preferred.

also, empty is pretty much self explanatory while =='\0' is not very much. when it's 2AM and you debug your code, would you prefer see if(str.empty()) or if(str[0] == '\0')? if only functionality matters, we would all write in vanilla assembly.

there is also a performance penalty involved. empty is usually implemented by comparing the size member of the string to zero, which is very cheap, easy to inline etc. comparing against the first character might be more heavy. first of all, since all strings implement short string optimization, the program first has to ask if the string is in "short mode" or "long mode". branching - worse performance. if the string is long, dereferencing it may be costly if the string was "ignored" for some time and the dereference itself may cause a cache-fault which is costly.

Solution 4 - C++

empty() is not implemented as looking for the existence of a null character at position 0, its simply

bool empty() const
{
    return size() == 0 ;
}

Which could be different

Solution 5 - C++

Also, beware of the functions you'll use if you use C++ 11 or later version:

#include <iostream>
#include <cstring>

int main() {
	std::string str("\0ab", 3);
	
	std::cout << "The size of str is " << str.size() << " bytes.\n";
	std::cout << "The size of str is " << str.length() << " long.\n";
	std::cout << "The size of str is " << std::strlen(str.c_str()) << " long.\n";

	return 0;
}

will return

> The size of str is 3 bytes. > > The size of str is 3 long. > > The size of str is 0 long.

Solution 6 - C++

You want to know the difference between str.empty() and str[0] == '\0'. Lets follow the example:

#include<iostream>
#include<string>
using namespace std;

int main(){
string str, str2; //both string is empty
str2 = "values"; //assigning a value to 'str2' string
str2[0] = '\0'; //assigning '\0' to str2[0], to make sure i have '\0' at 0 index

if(str.empty()) cout << "str is empty" << endl;
else cout << "str contains: " << str << endl;

if(str2.empty()) cout << "str2 is empty" << endl;
else cout << "str2 contains: " << str2 << endl;

return 0;
}

Output:

str is empty
str2 contains: alues

str.empty() will let you know the string is empty or not and str[0] == '\0' will let you know your strings 0 index contains '\0' or not. Your string variables 0 index contains '\0' doesn't mean that your string is empty. Yes, only once it can be possible when your string length is 1 and your string variables 0 index contains '\0'. That time you can say that, its an empty string.

Solution 7 - C++

C++ string has the concept of whether it is empty or not. If the string is empty then str[0] is undefined. Only if C++ string has size >1, str[0] is defined.

str[i] == '\0' is a concept of the C-string style. In the implementation of C-string, the last character of the string is '\0' to mark the end of a C-string.
For C-string you usually have to 'remember' the length of your string with a separate variable. In C++ String you can assign any position with '\0'.

Just a code segment to play with:

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char* argv[]) {
   char str[5] = "abc";
   cout << str << " length: " << strlen(str) << endl;
   cout << "char at 4th position: " << str[3] << "|" << endl;
   cout << "char at 5th position: " << str[4] << "|" << endl;
   str[4]='X'; // this is OK, since Cstring is just an array of char!
   cout << "char at 5th position after assignment: " << str[4] << "|" << endl;
   string cppstr("abc");
   cppstr.resize(3);
   cout << "cppstr: " << cppstr << " length: " << cppstr.length() << endl;
   cout << "char at 4th position:" << cppstr[3] << endl;
   cout << "char at 401th positon:" << cppstr[400] << endl;
   // you should be getting segmentation fault in the
   // above two lines! But this may not happen every time.

   cppstr[0] = '\0';
   str[0] = '\0';
   cout << "After zero the first char. Cstring: " << str << " length: " << strlen(str) << " | C++String: " << cppstr << " length: " << cppstr.length() << endl;
   return 0;
}

On my machine the output:

abc length: 3
char at 4th position: |
char at 5th position: |
char at 5th position after assignment: X|
cppstr: abc length: 3
char at 4th position:
char at 401th positon:?
After zero the first char. Cstring:  length: 0 | C++String: bc length: 3

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPro AccountView Question on Stackoverflow
Solution 1 - C++NathanOliverView Answer on Stackoverflow
Solution 2 - C++OberonView Answer on Stackoverflow
Solution 3 - C++David HaimView Answer on Stackoverflow
Solution 4 - C++MyDeveloperDayView Answer on Stackoverflow
Solution 5 - C++Thomas AyoubView Answer on Stackoverflow
Solution 6 - C++Anower PervesView Answer on Stackoverflow
Solution 7 - C++Kemin ZhouView Answer on Stackoverflow