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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With