C++ 将字符串初始化为 null 与空字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11556394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
initializing strings as null vs. empty string
提问by Saket
How would it matter if my C++ code (as shown below) has a string initialized as an empty string :
如果我的 C++ 代码(如下所示)有一个初始化为空字符串的字符串会有什么关系:
std::string myStr = "";
....some code to optionally populate 'myStr'...
if (myStr != "") {
// do something
}
vs. no/null initialization:
与无/空初始化:
std::string myStr;
....some code to optionally populate 'myStr'...
if (myStr != NULL) {
// do something
}
Are there any best practices or gotchas around this?
是否有任何最佳实践或问题?
回答by SingerOfTheFall
There's a function empty()
ready for you in std::string:
有一个功能empty()
为您准备好了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.
}
回答by juanchopanza
There are no gotchas. The default construction of std::string
is ""
. But you cannot compare a string to NULL
. The closest you can get is to check whether the string is empty or not, using the std::string::empty
method..
没有问题。的默认构造std::string
是""
。但是您不能将字符串与NULL
. 你能得到的最接近的是检查字符串是否为空,使用std::string::empty
方法..
回答by Luchian Grigore
Best:
最好的事物:
std::string subCondition;
This creates an empty string.
这将创建一个空字符串。
This:
这个:
std::string myStr = "";
does a copy initialization - creates a temporary string from ""
, and then uses the copy constructor to create myStr
.
进行复制初始化 - 从 中创建一个临时字符串""
,然后使用复制构造函数创建myStr
.
Bonus:
奖金:
std::string myStr("");
does a direct initialization and uses the string(const char*)
constructor.
直接初始化并使用string(const char*)
构造函数。
To check if a string is empty, just use empty()
.
要检查字符串是否为空,只需使用empty()
.
回答by Adam Romanek
Empty-ness and "NULL-ness" are two different concepts. As others mentioned the former can be achieved via std::string::empty()
, the latter can be achieved with boost::optional<std::string>
, e.g.:
Empty-ness 和“NULL-ness”是两个不同的概念。正如其他人提到的,前者可以通过 实现std::string::empty()
,后者可以通过 实现boost::optional<std::string>
,例如:
boost::optional<string> myStr;
if (myStr) { // myStr != NULL
// ...
}
回答by Andrew
I would prefere
我更喜欢
if (!myStr.empty())
{
//do something
}
Also you don't have to write std::string a = "";
. You can just write std::string a;
- it will be empty by default
你也不必写std::string a = "";
. 你可以写std::string a;
- 默认情况下它会是空的
回答by Jirka Hanika
The default constructor initializes the string to the empty string. This is the more economic way of saying the same thing.
默认构造函数将字符串初始化为空字符串。这是说同一件事的更经济的方式。
However, the comparison to NULL
stinks. That is an older syntax still in common use that means something else; a null pointer. It means that there is no string around.
然而,比较NULL
臭。这是一个仍然普遍使用的旧语法,它意味着别的东西;一个空指针。这意味着周围没有绳子。
If you want to check whether a string (that does exist) is empty, use the empty
method instead:
如果要检查字符串(确实存在)是否为空,请改用该empty
方法:
if (myStr.empty()) ...