C++ 如何使用“”初始化 std::string?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5354063/
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 initialize an std::string using ""?
提问by Mohsin
I'm facing problems with initializing a std::string variable using ""
(i.e. an empty string). It's causing strange behavior in code that was previously working. Is the following statement wrong?
我在使用""
(即空字符串)初始化 std::string 变量时遇到问题。它导致以前工作的代码出现奇怪的行为。下面的说法是错误的吗?
std::string operationalReason = "";
When I use the following code everything works fine:
当我使用以下代码时,一切正常:
std::string operationalReason;
operationalReason.clear();
I believe that string literals are stored in a separate memory location that is compiler-dependent. Could the problem I'm seeing actually be indicating a corruption of that storage? If so, it would get hidden by my usage of the clear()
function.
我相信字符串文字存储在依赖于编译器的单独内存位置。我看到的问题实际上是否表明该存储已损坏?如果是这样,它会被我对clear()
函数的使用隐藏起来。
Thanks.
谢谢。
采纳答案by Mark B
What happens if you just do std::string operationalReason;
? That should have the same effect as the two examples you provided. If in fact you're experiencing problems when you use the std::string operationalReason = "";
form that may indicate that the string data storage has been corrupted, but it may equally mean that some OTHER part of memory is corrupted and that particular line causes it to manifest differently.
如果你只是这样做会发生什么std::string operationalReason;
?这应该与您提供的两个示例具有相同的效果。如果实际上您在使用std::string operationalReason = "";
可能表明字符串数据存储已损坏的表单时遇到问题,但这同样可能意味着内存的某些 OTHER 部分已损坏,并且该特定行导致它以不同的方式显示。
Does your code crash immediately when you use the ""
form or later on at runtime? Are you able to run this under valgrind or similar to see if it spots memory problems? What happens if you initialized the string to some literal other than ""
?
当您使用""
表单或稍后在运行时,您的代码是否会立即崩溃?您是否能够在 valgrind 或类似程序下运行它以查看它是否发现内存问题?如果您将字符串初始化为除 之外的某个文字会发生什么""
?
回答by Nawaz
std::string operationalReason; //is enough!
It invokes the default constructor, and creates an empty string anyway.
它调用默认构造函数,并创建一个空字符串。
So I would say std::string operationalReason = ""
is overkill.
所以我会说std::string operationalReason = ""
是矫枉过正。
回答by Armen Tsirunyan
std::string operationalReason = "";
std::string operationalReason = "";
This is perfectly fine, technically, but more common and nice is just
从技术上讲,这完全没问题,但更常见和更好的是
std::string operationalReason;
std::string operationalReason;
The default ctor of the string will create an empty string
字符串的默认ctor将创建一个空字符串
Yes, you are right about string literals being stored in a nonmutable memory blah blah etc etc... but the string copy-ctor always copies the string or C-string passed
是的,您对存储在非可变内存中等等等等的字符串文字是正确的……但是字符串复制构造函数总是复制传递的字符串或 C 字符串
回答by mik
The two forms are corrected, but this one:
两种形式都得到了纠正,但这个:
std::string operationalReason = ""
It calls the constructor which takes a const char *
as argument. First it calls the default constructor and then it tries to copy the data, in this case nothing.
它调用以 aconst char *
作为参数的构造函数。首先它调用默认构造函数,然后它尝试复制数据,在这种情况下什么也没有。
std::string operationalReason;
is preferable.
You can use clear()
to reset it to an empty string.
是可取的。您可以使用clear()
将其重置为空字符串。