Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么#包含<;字符串>;需要字符串对象,但不需要字符串文字?_C++_String_Header Files - Fatal编程技术网

C++ 为什么#包含<;字符串>;需要字符串对象,但不需要字符串文字?

C++ 为什么#包含<;字符串>;需要字符串对象,但不需要字符串文字?,c++,string,header-files,C++,String,Header Files,为什么对变量和参数使用string对象时,您需要#include,但对字符串文本不需要它?例如,您可以说cout字符串文本不是std::string对象,它是const char的数组 “这是一个字符串文本”的类型为常量字符[25] 在大多数情况下——包括这一种情况——数组隐式衰减为指向其第一个元素的指针,并且有一个操作符一个字符串文本,如“this a string literal”是const char[25],而不是std::string类型。语句cout库为std::ostream&和c

为什么对变量和参数使用string对象时,您需要
#include
,但对字符串文本不需要它?例如,您可以说
cout字符串文本不是
std::string
对象,它是
const char
的数组
“这是一个字符串文本”
的类型为
常量字符[25]


在大多数情况下——包括这一种情况——数组隐式衰减为指向其第一个元素的指针,并且有一个
操作符一个字符串文本,如
“this a string literal”
const char[25]
,而不是
std::string
类型。语句
cout库为
std::ostream&
const char*
提供了一个重载二进制文件(顺便说一句,它们有时会意外地出现)。旁注:如果我没记错的话,Deitel父子。字符串文字不是
String
s。它们是恒定的字符数组。
std::cout
设计用于将字符数组打印为字符串。Fwiw,您可以使用
“foo”s
获取字符串、字符串literal@NathanOliver是的,但是要使用这种语法,您需要C++14或更高版本,并且仍然需要
\include