C++ 中未初始化的 std::string 变量的值和大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17738439/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:24:22  来源:igfitidea点击:

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

c++stdstring

提问by Charzhard

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?

未初始化的字符串“name”的值是多少?它的大小是多少?

回答by Pierre Fourgeaud

Because it is not initialized, it is the default constructor that is called. Then :

因为它没有初始化,所以调用的是默认构造函数。然后 :

empty string constructor (default constructor) :

Constructs an empty string, with a length of zero characters.

空字符串构造函数(默认构造函数):

构造一个空字符串,长度为零字符。

Take a look : http://www.cplusplus.com/reference/string/string/string/

看一看:http: //www.cplusplus.com/reference/string/string/string/

EDIT :As stated in C++11, §21.4.2/1:

编辑:C++11, §21.4.2/1 所述

E?ects: Constructs an object of class basic_string. The postconditions of this function are indicated in Table 63.

效果:构造一个basic_string类的对象。该函数的后置条件如表 63 所示。

-> Table 63
+-----------------------------------------------------------------------------+
| data()     | a non-null pointer that is copyable and can have 0 added to it |
+------------+----------------------------------------------------------------+
| size()     | 0                                                              |
+------------+----------------------------------------------------------------+
| capacity() | an unspeci?ed value                                            |
+-----------------------------------------------------------------------------+

回答by Oliver Charlesworth

It's not uninitialized, its default constructoris called.

它不是未初始化的,它的默认构造函数被调用。

From http://en.cppreference.com/w/cpp/string/basic_string/basic_string:

http://en.cppreference.com/w/cpp/string/basic_string/basic_string

Default constructor. Constructs empty string.

默认构造函数。构造空字符串。

回答by Rapptz

Default constructed user-defined types are not uninitialized. The default constructor defines an empty string (i.e "") with a size/length of zero.

默认构造的用户定义类型不是未初始化的。默认构造函数定义了""一个大小/长度为零的空字符串(即)。

回答by jogojapan

The Standard (C++11, §21.4.2/1) describes the results of default-constructing a std::basic_string(of which std::stringis a specialization) as follows:

标准(C++11,第 21.4.2/1 节)描述了默认构造 a std::basic_string(其中std::string一个特化)的结果如下:

[...] an object of class basic_string. The postconditions [...] are indicated in Table 63.

[...] 类的对象basic_string。后置条件 [...] 如表 63 所示。

And Table 63 says:

表 63 说:

data()a non-null pointer that is copyable and can have 0 added to it
size()0
capacity()an unspecified value

data()一个非空指针,它是可复制的并且可以添加
size()0 0
capacity()一个未指定的值

回答by Anand Rathi

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

value 为 null ,size 为 0 但是你可以通过 empty() 直接 chk 字符串是否为空

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

类似且更详细的讨论是 将字符串初始化为 null 与空字符串