简体   繁体   English

对stl的头文件

[英]Header file for pair stl

I am used to writing codes using stl pair without including any specific header file for using pair. 我习惯使用stl对编写代码而不包括任何使用对的特定头文件。 But a friend today told me that I should use utility header 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. 如果我可以在不使用它的情况下编写代码,那么实用程序头的用途是什么。

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::pair you should add #include <utility> . 您需要阅读类的引用(例如在cppreference.com上 - http://en.cppreference.com/w/cpp/utility/pair )并检查您需要包含哪个头文件 - 如果是std::pair你应该添加#include <utility> You cannot depend on the fact, that, for example, iostream already includes iomanip and your code compiles when you use manipulators like setw etc. You cannot - you always should refer to language specs and include required headers. 您不能依赖于这样的事实,例如, iostream已经包含iomanip并且当您使用setw等操纵器时,您的代码会编译。您不能 - 您总是应该参考语言规范并包含所需的标题。

The point is that you may have had indirectly included the <utility> header through the inclusion of some other header. 关键是你可能通过包含一些其他标头间接包含<utility>标头。 Usually it is the case that headers are included by other headers in a C++ implementation without that inclusion being mandated by the standard. 通常情况下,标头包含在C ++实现中的其他标头中,而标准不强制要求包含标头。 So, by including <utility> , you make sure your code is portable across standard compliant implementations (at least with respect to this particular issue). 因此,通过包含<utility> ,您可以确保您的代码可以跨标准兼容的实现移植(至少就此特定问题而言)。

The standard specifies that std::pair is in <utility> , so you should include this whenever you use an std::pair . 该标准指定std::pair<utility> ,因此每当使用std::pair时都应该包含它。

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::pair is made available by <utility> . 类模板std::pair<utility>

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

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