C++ 如何检查是否设置了 std::string?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3903587/
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
How to check if a std::string is set or not?
提问by nakiya
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
? How to check if the string is set or not?
如果使用 a char*
,我可以将其初始化为NULL
,然后通过比较来检查它是否已设置。如何为 a 做同样的事情std::string
?如何检查字符串是否设置?
EDIT:
What if the string I set to is also empty? Do I have to use an additional flag to check if the std::string
is set or not?
编辑:如果我设置的字符串也是空的怎么办?我是否必须使用额外的标志来检查是否std::string
已设置?
回答by GManNickG
Use empty()
:
使用empty()
:
std::string s;
if (s.empty())
// nothing in s
回答by Michael Burr
As several answers pointed out, std::string
has no concept of 'nullness' for its value. If using the empty string as such a value isn't good enough (ie., you need to distinguish between a string that has no characters and a string that has no value), you can use a std::string*
and set it to NULL or to a valid std::string
instance as appropriate.
正如几个答案所指出的那样,std::string
它的价值没有“空性”的概念。如果使用空字符串作为这样的值不够好(即,您需要区分没有字符的字符串和没有值的字符串),您可以使用 astd::string*
并将其设置为 NULL 或 astd::string
适当的有效实例。
You may want to use some sort of smart pointer type (boost::scoped_ptr or something) to help manage the lifetime of any std::string
object that you set the pointer to.
您可能希望使用某种智能指针类型(boost::scoped_ptr 或其他类型)来帮助管理std::string
您设置指针的任何对象的生命周期。
回答by James McNellis
You can't; at least not the same way you can test whether a pointer is NULL
.
你不能;至少与测试指针是否为NULL
.
A std::string
object is always initialized and always contains a string; its contents by default are an empty string (""
).
一个std::string
对象总是被初始化并且总是包含一个字符串;默认情况下,其内容是空字符串 ( ""
)。
You can test for emptiness (using s.size() == 0
or s.empty()
).
您可以测试是否为空(使用s.size() == 0
或s.empty()
)。
回答by Employed Russian
There is no "unset" state for std::string
, it is alwaysset to something.
没有“未设置”状态std::string
,它总是被设置为某些东西。
回答by verisimilidude
The default constructor for std::string always returns an object that is set to a null string.
std::string 的默认构造函数总是返回一个设置为空字符串的对象。
回答by PatrickV
I don't think you can tell with the std::string class. However, if you really need this information, you could always derive a class from std::string and give the derived class the ability to tell if it had been changed since construction (or some other arbitrary time). Or better yet, just write a new class that wraps std::string since deriving from std::string may not be a good idea given the lack of a base class virtual destructor. That's probably more work, but more work tends to be needed for an optimal solution.
我认为你不能用 std::string 类来判断。但是,如果您确实需要此信息,您始终可以从 std::string 派生一个类,并赋予派生类判断自构造(或其他任意时间)以来它是否已更改的能力。或者更好的是,只需编写一个包装 std::string 的新类,因为由于缺少基类虚拟析构函数,从 std::string 派生可能不是一个好主意。这可能需要更多的工作,但往往需要更多的工作才能获得最佳解决方案。
Of course, you can always just assume if it contains something other than "" then it has been "set", this won't detect it manually getting set to "" though.
当然,您总是可以假设它是否包含“”以外的其他内容,那么它已被“设置”,但这不会检测到它手动设置为“”。