How to check if a std::string is set or not?

前端 未结 6 775
清酒与你
清酒与你 2021-01-31 06:53

If using a char*, I can initialize it to NULL and later check if it is set by doing a comparison. How to do the same thing for a std::string

6条回答
  •  庸人自扰
    2021-01-31 07:34

    You can't; at least not the same way you can test whether a pointer is NULL.

    A std::string object is always initialized and always contains a string; its contents by default are an empty string ("").

    You can test for emptiness (using s.size() == 0 or s.empty()).

提交回复
热议问题