Why is map imported as #include <map>
, but stdio imported as #include <stdio.h>
?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
It's just the way it's defined by the C++ Standard -- as it happens,
map
andstdio.h
don't even have to be real files.As a side-note,
stdio.h
is the header that was originally imported into C++ from the C standard libraries -- the C++ version iscstdio
. In practical terms, this generally means that when you includecstdio
instead, you get the stuff fromstdio.h
, but it's in namespace std.Just to clarify: the
stdio.h
you include in C++ is the C++ version of what was originally a C header. But the C++ way of writing the include iscstdio
.All standard C++ headers don't want the
.h
in the end. I read somewhere that the concept is that they don't need to be actual files,even if I never saw an implementation do it in another manneredit: actually the compiler intrinsics should work considering the headers included but not actually including them as files; see @Yttrill's comment.For the
stdio.h
thing, in a C++ application you shouldn't include<stdio.h>
, but you should instead include<cstdio>
. In general, you shouldn't include the "normal" C headers, but their C++-ized counterparts, which haven't got the.h
in the end, have ac
in front and put all the symbols defined in them in thestd
namespace. So,<math.h>
becomes<cmath>
,<stdlib.h>
becomes<cstdlib>
, and so on.In general, you should use the C++-ized versions of C headers both to avoid to pollute the global namespace (assuming you're not one of those guys who put
using namespace std;
everywhere) and to benefit of some C++ improvements to the standard C headers (e.g. added overloading to some math functions).In general, the implementation of this whole thing is simply done by having such files without extension in the directory in which the compiler looks for the header files. In my g++ 4.4 installation, for example, you have:
The C++-ized C headers in theory could just be a
but in general they are more complicated to deal with implementation-specific problems (especially regarding macros) and to add C++-related functionality (see e.g. the previous example of added overloads in
<cmath>
).By the way, the C++ standard (§D.5) do not say that the
<c***>
headers should behave as if they included the<***.h>
headers in anamespace std
directive, but the opposite:Notice that such headers are considered deprecated (§C.2.1), so this is the main reason you shouldn't use them:
It's simply the name of the actual file on disk. There is (probably) no file called
map.h
orstdio
in your standard include directory.The C++ standard library moved away from the previous style of using
.h
toward not having.h
at the end of the file names. This may possibly have been related to making the syntax look more like templates:(Preemptive comment: Yes, I know the above needs
std::
to build, but it's just an illustration.)