gpt4 book ai didi

c++ - 为什么 cout 需要标题字符串?

转载 作者:太空狗 更新时间:2023-10-29 19:42:24 24 4
gpt4 key购买 nike

为什么在下面的代码中不包含“字符串” header 我可以声明字符串变量。但是当我尝试打印字符串时,编译器只针对 cout 提示。

“字符串”头包含哪些信息?

  #include <iostream>
//#include "string"

int main ()
{
std::string str="SomeWorld";

std::cout<<str<<std::endl;

return 0;
}

最佳答案

因为标题定义了std::basic_string很可能(间接)包含在 <iostream> 中( std::string 是基于 std::basic_string<char> 的类型定义)。 operator<< 的过载对于 std::cout然而仅在 <string> 中定义.

关于c++ - 为什么 cout 需要标题字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6817984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com