简体   繁体   English

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

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

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”的值是多少,它的大小是多少?

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中所述

Effects: Constructs an object of class basic_string . E ff ects:构造一个basic_string类的对象。 The postconditions of this function are indicated in Table 63. 该函数的后置条件如表63所示。

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

It's not uninitialized, its default constructor is 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. 构造空字符串。

Default constructed user-defined types are not uninitialized. 默认构造的用户定义类型不是未初始化的。 The default constructor defines an empty string (ie "" ) with a size/length of zero. 默认构造函数定义一个大小/长度为零的空字符串(即"" )。

The Standard (C++11, §21.4.2/1) describes the results of default-constructing a std::basic_string (of which std::string is a specialization) as follows: 标准(C ++ 11,§21.4.2/ 1)描述了默认构造std::basic_string (其中std::string是一个特化)的结果,如下所示:

[...] an object of class basic_string . [...]类basic_string的对象。 The postconditions [...] are indicated in Table 63. 后置条件[...]如表63所示。

And Table 63 says: 表63说:

data() a non-null pointer that is copyable and can have 0 added to it data()一个非空指针,可以复制并且可以添加0
size() 0 size() 0
capacity() an unspecified value capacity()未指定的值

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与空字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM