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

前端 未结 7 2226
迷失自我
迷失自我 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:43

    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.

提交回复
热议问题