c ++中未初始化的std :: string变量的值和大小 [英] Value and size of uninitialized std::string variable in c++
本文介绍了c ++中未初始化的std :: string变量的值和大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果字符串定义如下
std::string name;
未初始化的字符串name的值将是多少? p>
What will be the value of the uninitialized string "name" and what size it would be?
推荐答案
因为它没有被初始化,它是被调用的默认构造函数。然后:
Because it is not initialized, it is the default constructor that is called. Then :
空字符串构造函数(默认构造函数):
构造一个长度为零字符的空字符串。
Constructs an empty string, with a length of zero characters.
查看:http://www.cplusplus.com/reference/string/string/string/
EDIT:
如 C ++ 11,§21.4.2/ 1 中所述:
E ff ects:构造类 basic_string 的对象。该函数的后置条件如表63所示。
Effects: Constructs an object of class basic_string. The postconditions of this function are indicated in Table 63.
-> Table 63
+-----------------------------------------------------------------------------+
| data() | a non-null pointer that is copyable and can have 0 added to it |
+------------+----------------------------------------------------------------+
| size() | 0 |
+------------+----------------------------------------------------------------+
| capacity() | an unspecified value |
+-----------------------------------------------------------------------------+
这篇关于c ++中未初始化的std :: string变量的值和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文