INT_MIN
and INT_MAX
are not part of the <algorithm>
header. They are actually defined in the <climits>
header (or <limits.h>
in C). If you're not including this header but are still able to use INT_MIN
and INT_MAX
, it's likely that some other included header internally includes <climits>
.
Your issue could be because of differences in compiler settings, or perhaps the lecturer's code has other includes that indirectly bring in INT_MIN
and INT_MAX
.
To use INT_MIN
and INT_MAX
explicitly, include the <climits>
header.
#include <climits>
int main() {
int min_val = INT_MIN;
int max_val = INT_MAX;
}
That should resolve the compiler error. Note using, std::numeric_limits
is more type-safe and flexible. It works with custom numeric types and is more in line with C++ idioms. It's part of the <limits>
header and allows you to query properties for all built-in types and any user-defined types that specialize std::numeric_limits
.
Example:
#include <limits>
int main() {
int max_int = std::numeric_limits<int>::max();
int min_int = std::numeric_limits<int>::min();
short max_short = std::numeric_limits<short>::max();
}
Using std::numeric_limits
is considered idiomatic in modern C++ and offers advantages in terms of type safety and code maintainability.