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

前端 未结 7 2235
迷失自我
迷失自我 2021-02-11 11:43

Suppose we have a string

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

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

相关标签:
7条回答
  • 2021-02-11 12:21

    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'.

    0 讨论(0)
  • 2021-02-11 12:31

    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).

    0 讨论(0)
  • 2021-02-11 12:32

    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
    
    0 讨论(0)
  • 2021-02-11 12:32

    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

    0 讨论(0)
  • 2021-02-11 12:40

    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.

    0 讨论(0)
  • 2021-02-11 12:43

    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.

    0 讨论(0)
提交回复
热议问题