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

前端 未结 6 1655
予麋鹿
予麋鹿 2021-02-11 12:15

Suppose we have a string

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

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

6条回答
  •  走了就别回头了
    2021-02-11 12:24

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

    #include 
    #include 
    
    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.

提交回复
热议问题