std::numeric_limits::max

定义于头文件 <limits>
static T max();
(C++11 前)
static constexpr T max();
(C++11 起)

返回数值类型 T 所能表示的最大有限值。对所有有界类型有意义。

目录

返回值

T std::numeric_limits<T>::max()
/* non-specialized */ T();
bool true
char CHAR_MAX
signed char SCHAR_MAX
unsigned char UCHAR_MAX
wchar_t WCHAR_MAX
char16_t UINT_LEAST16_MAX
char32_t UINT_LEAST32_MAX
short SHRT_MAX
unsigned short USHRT_MAX
int INT_MAX
unsigned int UINT_MAX
long LONG_MAX
unsigned long ULONG_MAX
long long LLONG_MAX
unsigned long long ULLONG_MAX
float FLT_MAX
double DBL_MAX
long double LDBL_MAX

异常

(无) (C++11 前)
noexcept 规定:  
noexcept
  
(C++11 起)

示例

以一些基本类型和一些标准库 typedef 演示 max() 的使用(输出是系统限定的)

#include <limits>
#include <cstddef>
#include <iostream>
int main()
{
    std::cout << "short: " << std::dec << std::numeric_limits<short>::max()
              << " or " << std::hex << std::showbase << std::numeric_limits<short>::max() << '\n'
              << "int: " << std::dec << std::numeric_limits<int>::max()
              << " or " << std::hex << std::numeric_limits<int>::max() << '\n' << std::dec
              << "streamsize: " << std::dec << std::numeric_limits<std::streamsize>::max()
              << " or " << std::hex << std::numeric_limits<std::streamsize>::max() << '\n'
              << "size_t: " << std::dec << std::numeric_limits<std::size_t>::max()
              << " or " << std::hex << std::numeric_limits<std::size_t>::max() << '\n'
              << "float: " << std::numeric_limits<float>::max()
              << " or " << std::hexfloat << std::numeric_limits<float>::max() << '\n'
              << "double: " << std::defaultfloat << std::numeric_limits<double>::max()
              << " or " << std::hexfloat << std::numeric_limits<double>::max() << '\n';
}

输出:

short: 32767 or 0x7fff
int: 2147483647 or 0x7fffffff
size_t: 18446744073709551615 or 0xffffffffffffffff
streamsize: 9223372036854775807 or 0x7fffffffffffffff
float: 3.40282e+38 or 0x1.fffffep+127
double: 1.79769e+308 or 0x1.fffffffffffffp+1023

参阅

[静态] (C++11)
返回给定类型的最低有限值
(公开静态成员函数)
[静态]
返回给定类型的最小有限值
(公开静态成员函数)

版本历史

  • (当前 | 先前 2017年7月17日 (一) 05:03Fruderica讨论 | 贡献. . (3,269字节) (-362). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 18:17P12bot讨论 | 贡献 . . (3,631字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:49P12bot讨论 | 贡献 . . (3,631字节) (-290). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 14:51P12bot讨论 | 贡献 . . (3,921字节) (+289). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 12:12P12讨论 | 贡献 . . (3,632字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (3,632字节) (-36). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 03:54P12讨论 | 贡献 . . (3,668字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (3,668字节) (+3,668). . (Translated from the English version using Google Translate)