initializing strings as null vs. empty string

后端 未结 6 715
独厮守ぢ
独厮守ぢ 2020-12-24 01:07

How would it matter if my C++ code (as shown below) has a string initialized as an empty string :

std::string myStr = \"\";
....some code to optionally popul         


        
6条回答
  •  粉色の甜心
    2020-12-24 01:13

    There's a function empty() ready for you in std::string:

    std::string a;
    if(a.empty())
    {
        //do stuff. You will enter this block if the string is declared like this
    }
    

    or

    std::string a;
    if(!a.empty())
    {
        //You will not enter this block now
    }
    a = "42";
    if(!a.empty())
    {
        //And now you will enter this block.
    }
    

提交回复
热议问题