问题
Can somebody please explain this?
#include <iostream>
#include <limits.h>
or
#include <iostream>
#include <limits>
回答1:
<limits> is a C++ Standard Library header providing similar insights to the C header <limits.h> (which is also available in C++ as <climits>), but it is written in a way that's more useful and safe in C++ programs:
say you have a
template <typename Numeric> ..., and the code inside wants to know the minimum and maximum value of theNumerictype parameter that the user instantiated your template with: you can usestd::numeric_limits<Numeric>::min()and...::max(); if you wanted to access the same values from<climits>, it'd be hard to know which ofSCHAR_MIN,SHRT_MIN,INT_MIN,LONG_MINetc. to use and you'd have to switch between them all yourself - lots of extra code for something so trivial<climits>has lots of macros, and macros don't respect namespaces or scopes the way "normal" C++ identifiers do - their substitutions are made pretty indescriminately - so they make your program more error prone<limits>gives much more insight about numeric types, such as whether they're signed, the number of base-10 digits they can handle, whether they can represent infinity or not-a-number sentinel values etc. (see the header docs for a fuller list and information)
回答2:
limits.h is a C standard library header. limits is a C++ standard library header. They contain different things.
There is climits in C++, which offers more or less what limits.h did.
来源:https://stackoverflow.com/questions/36831465/what-difference-does-it-make-when-i-include-limits-or-limits-h-in-my-c-cod