简体   繁体   English

为什么cout需要头字符串?

[英]Why header string is required for cout?

Why in the below code without including "string" header I can declare string variable. 为什么在下面的代码中没有包含“string”标题我可以声明字符串变量。 But compiler complains only for cout, when I try to print the string. 但是当我尝试打印字符串时,编译器只对cout抱怨。

What information "string" header consists of ? “字符串”标题包含哪些信息?

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

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

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

  return 0;
}

Because the header defining std::basic_string is most likely (indirectly) included by <iostream> ( std::string is a typedef based on std::basic_string<char> ). 因为定义std::basic_string的头很可能(间接地)包含在<iostream>std::string是一个基于std::basic_string<char>的typedef)。 The overload for operator<< for std::cout however is only defined in <string> . 但是, operator<< for std::cout的重载仅在<string>定义。

它不是std::cout所必需的,它是std::string必需的。

Strictly speaking, anything could happen unless you include all the correct headers. 严格来说,除非包含所有正确的标题,否则任何事情都可能发生。 There is no mandatory inclusion of one standard header into any other. 没有强制将一个标准标题包含在任何其他标准标题中。 So to be portable and correct, you have to say this: 所以为了便携和正确,你必须这样说:

#include <string>    // for `std::string`
#include <ostream>   // for `std::ostream &` in `operator<<`
#include <iostream>  // for std::cout

int main() {
  std::string str = "hello world";
  std::cout << str << std::endl;
}

In any real implementation you can almost always get away with omitting some of the headers (eg ostream would probably have been included in iostream ), but the above is the standard-compliant way. 在任何实际的实现中,您几乎总是可以省略一些标题(例如, ostream可能已包含在iostream ),但以上是符合标准的方式。

The <string> header includes the definition of the string class (note: #include "string" means to include the string file in the current directory, so you should use angle brackets instead of " for system includes.) <string>标头包含string类的定义(注意: #include "string"表示将string文件包含在当前目录中,因此您应该使用尖括号而不是" for system includes”。)

However, iostream already includes string , (for example, to declare the operator<< that works for std::string ) so this is why you don't need to include it in this case. 但是, iostream已经包含了string ,(例如,声明operator<<适用于std::string )所以这就是为什么在这种情况下你不需要包含它。

In any case, it is a good practice to include the headers you just need. 无论如何,最好包含您需要的标题。 This makes your code more portable, and more explicit in case you copy that code into another context, say, that don't include iostream as a previous include. 这使得您的代码更具可移植性,并且在您将该代码复制到另一个上下文(例如,不包含iostream作为先前的包含)的情况下更加明确。 Also, note that it is never specified that, for example, including iostream will make std::string available, so strictly speaking, you have to include string to use std::string . 另外,请注意,永远不会指定,例如,包括iostream将使std::string可用,因此严格来说,您必须包含string才能使用std::string

Because <string> is included in <iostream> . 因为<string>包含在<iostream> That is why the compiler is not complaning. 这就是编译器没有抱怨的原因。

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

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