Header file for pair stl
Solution 1
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>
. 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.
Solution 2
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).
The standard specifies that std::pair
is in <utility>
, so you should include this whenever you use an std::pair
.
Solution 3
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>
.
Related videos on Youtube
user1543957
Updated on July 09, 2022Comments
-
user1543957 almost 2 years
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 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.