C++ 对 stl 的头文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13553872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:27:41  来源:igfitidea点击:

Header file for pair stl

c++stl

提问by user1543957

I am used to writing codes using stl pair without including any specific header file for using pair. But a friend today told me that I should use utilityheader whenever I use pair else I will have problem on some compilers. Please tell if this is true. And what is the use of utility header if I can write codes without using it.

我习惯于使用 stl pair 编写代码,而不包含任何用于 using pair 的特定头文件。但是今天的一个朋友告诉我,每当我使用 pair 时我都应该使用实用程序头文件,否则我会在某些编译器上遇到问题。请告知这是不是真的。如果我可以在不使用它的情况下编写代码,那么实用程序头有什么用。

回答by Maciek

You should almost always include header file for every class which you use in your program, otherwise you depend on the fact that some headers internally use a class of your interest, but this can change on another compiler or version. You need to read reference of a class (for example on cppreference.com - http://en.cppreference.com/w/cpp/utility/pair) and check which header file you need to include - in case of std::pairyou should add #include <utility>. You cannot depend on the fact, that, for example, iostreamalready includes iomanipand your code compiles when you use manipulators like setwetc. You cannot - you always should refer to language specs and include required headers.

您应该几乎总是为您在程序中使用的每个类包含头文件,否则您依赖于某些头文件在内部使用您感兴趣的类的事实,但这可能会在另一个编译器或版本上发生变化。您需要阅读类的参考(例如在 cppreference.com - http://en.cppreference.com/w/cpp/utility/pair 上)并检查您需要包含哪个头文件 - 如果std::pair您应该添加#include <utility>. 您不能依赖这样一个事实,例如,当您使用诸如等操作器时,您的代码iostream已经包含iomanip并且您的代码会编译setw。您不能 - 您应该始终参考语言规范并包含所需的标头。

回答by juanchopanza

The point is that you may have had indirectly included the <utility>header through the inclusion of some other header. Usually it is the case that headers are included by other headers in a C++ implementation without that inclusion being mandated by the standard. So, by including <utility>, you make sure your code is portable across standard compliant implementations (at least with respect to this particular issue).

关键是您可能<utility>通过包含其他标题间接包含了标题。通常情况下,C++ 实现中的其他标头包含标头,而标准没有强制要求包含该标头。因此,通过包含<utility>,您可以确保您的代码在符合标准的实现中是可移植的(至少对于这个特定问题)。

The standard specifies that std::pairis in <utility>, so you should include this whenever you use an std::pair.

标准指定了std::pairin <utility>,因此无论何时使用std::pair.

回答by Dietmar Kühl

You always need to include the headers defining the components you use. Some standard libraries will be implemented to include other declarations they use internally butyou can't rely on this, at all. I consider it an error that standard libraries make declarations available they are not required to make available.

您始终需要包含定义您使用的组件的标题。将实现一些标准库以包含它们内部使用的其他声明,但您根本不能依赖于此。我认为标准库使声明可用是错误的,它们不需要提供。

The class template std::pairis made available by <utility>.

类模板std::pair由 提供<utility>