C ++中未初始化的std :: string变量的值和大小 [英] Value and size of an uninitialized std::string variable in c++

查看:192
本文介绍了C ++中未初始化的std :: string变量的值和大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这样定义一个字符串

If a string is defined like this

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/

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


作用:构造 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆