Value and size of an uninitialized std::string variable in c++

前端 未结 5 1587
既然无缘
既然无缘 2020-12-09 07:44

If a string is defined like this

std::string name;

What will be the value of the uninitialized string \"name\" and what size it would be?

5条回答
  •  死守一世寂寞
    2020-12-09 08:27

    value is null , and size is 0 But you can directly chk if the string is empty or not by empty()

    Just in case you want to check that in your application , Do this

    std::string name // Construct an empty string  
    if(name.empty()) { // Check if its empty
      name="something";
    }
    

    Similar and more detailed discussion is here initializing strings as null vs. empty string

提交回复
热议问题