Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header file for pair stl

Tags:

c++

stl

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.

like image 955
user1543957 Avatar asked Nov 25 '12 18:11

user1543957


2 Answers

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.

like image 106
Maciek Avatar answered Oct 19 '22 04:10

Maciek


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.

like image 34
juanchopanza Avatar answered Oct 19 '22 03:10

juanchopanza